插入记录,我们通常会用sql语句“INSERT INTO”,但是在Access的查询中,insert into 只能一次插入一条记录。那么如果同时要录入多条记录的话,就要写多个查询。当然这多个查询这种方法是不可取的。
那么该如何实现在表A中一次性插入(保存)窗体中的所有记录。一般情况下 我们是使用ADO或者DAO 来读取记录集,再写入表的。
但是能不能在一个查询中实现多条记录的追加呢?答案是肯定的
Insert INTO students
Select DISTINCT ROW *
FROM (Select 1 AS ID,"张三" AS XM,"男" AS XB,"12" AS BJ FROM MSysObjects UNION
Select 2 AS ID,"李四" AS XM,"女" AS XB,"13" AS BJ FROM MSysObjects UNION
Select 3 AS ID,"王五" AS XM,"女" AS XB,"14" AS BJ FROM MSysObjects UNION
Select 4 AS ID,"赵六" AS XM,"男" AS XB,"15" AS BJ FROM MSysObjects UNION
Select 5 AS ID,"孙七" AS XM,"女" AS XB,"16" AS BJ FROM MSysObjects UNION
Select 6 AS ID,"吴八" AS XM,"男" AS XB,"17" AS BJ FROM MSysObjects) AS temp;
说明:此处的 students 是一个表,字段有:ID,XM,XB,BJ。MsysObjects 是系统表,只要创建对象,此表中就一定有记录,此表也可以用其它表代替,不过要保证表中至少有一条记录,至于记录的内容在此无关紧要。
另一写法:
Insert INTO students
Select *
FROM (Select TOP 1 1 AS ID,"张三" AS XM,"男" AS XB,"12" AS BJ FROM MSysObjects UNION
Select TOP 1 2 AS ID,"李四" AS XM,"女" AS XB,"13" AS BJ FROM MSysObjects UNION
Select TOP 1 3 AS ID,"王五" AS XM,"女" AS XB,"14" AS BJ FROM MSysObjects UNION
Select TOP 1 4 AS ID,"赵六" AS XM,"男" AS XB,"15" AS BJ FROM MSysObjects UNION
Select TOP 1 5 AS ID,"孙七" AS XM,"女" AS XB,"16" AS BJ FROM MSysObjects UNION
Select TOP 1 6 AS ID,"吴八" AS XM,"男" AS XB,"17" AS BJ FROM MSysObjects) AS temp;