Multi-row insert 自 SQL-92 以来一直是 SQL 标准的一部分,并且许多现代 DBMS 都支持它。这将允许您执行以下操作:
insert into MyTable ( Name, Id, Location)
values ('John', 123, 'Lloyds Office'),
('Jane', 124, 'Lloyds Office'),
('Billy', 125, 'London Office'),
('Miranda', 126, 'Bristol Office');
您会注意到我在那里使用了insert into 的完整 形式,列出了要使用的列。我更喜欢这样,因为它使您不受列默认顺序的影响。
如果您的特定 DBMS 不支持它,您可以将其作为依赖于 DBMS 的事务的一部分来执行,但基本上看起来像:
begin transaction;
insert into MyTable (Name,Id,Location) values ('John',123,'Lloyds Office');
insert into MyTable (Name,Id,Location) values ('Jane',124,'Lloyds Office'),
insert into MyTable (Name,Id,Location) values ('Billy',125,'London Office'),
insert into MyTable (Name,Id,Location) values ('Miranda',126,'Bristol Office');
commit transaction;
这使得操作原子化,要么插入所有值,要么不插入。