天天看點

用一條sql語句同時插入多行資料

例如:

insert into tbl_stu(stu_id,stu_name,stu_sex,stu_age) VALUES ('1003','周宏偉','男','12');

insert into tbl_stu(stu_id,stu_name,stu_sex,stu_age) VALUES ('1004','何小飛','女','43');

insert into tbl_stu(stu_id,stu_name,stu_sex,stu_age) VALUES ('1005','李華','女','15');

insert into tbl_stu(stu_id,stu_name,stu_sex,stu_age) VALUES ('1006','趙婷','女','31');

如何将這樣繁瑣的sql語句整理為一句去執行?

insert into tbl_stu(stu_id,stu_name,stu_sex,stu_age)  
select '1003','周宏偉','男','12' union
select '1004','何小飛','女','43' union
select '1005','李華','女','15' union
select '1006','趙婷','女','31'
 
 
--SQL 2008
insert into tbl_stu(stu_id,stu_name,stu_sex,stu_age) 
VALUES ('1003','周宏偉','男','12'),
VALUES ('1004','何小飛','女','43'),
VALUES ('1005','李華','女','15'),
VALUES ('1006','趙婷','女','31')
           

 用union all效率更高一些。