我正在使用MS Access 2003.我想在MS Access中的"查询"中运行大量插入SQL语句.有没有简单的(或实际上有任何方式)做到这一点?
是的,不.
你做不到:
insert into foo (c1, c2, c3) values ("v1a", "v2a", "v3a"), ("v1b", "v2b", "v3b"), ("v1c", "v2c", "v3c")
但你可以做到
insert into foo (c1, c2, c3) select (v1, v2, v3) from bar
如果您还没有表中的数据,那会给您带来什么?那么,你可以制作一个Select语句,它由许多Selects联合组成,带有硬编码结果.
INSERT INTO foo (f1, f2, f3) SELECT * FROM (select top 1 "b1a" AS f1, "b2a" AS f2, "b3a" AS f3 from onerow union all select top 1 "b1b" AS f1, "b2b" AS f2, "b3b" AS f3 from onerow union all select top 1 "b1c" AS f1, "b2c" AS f2, "b3c" AS f3 from onerow)
注意:我还必须包含某种形式的虚拟表(例如,onerow)来欺骗访问允许联合(它必须至少有一行),并且你需要"前1"以确保你不要对于包含多行的表格,我会重复一遍
但话说回来,只做三个单独的插入语句可能会更容易,特别是如果你已经在循环中构建了东西(当然除非你的插入成本高于你编写代码的成本) .