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 表名(列名,列名...列名)
select 值1,值1,值1...值1 union
select 值2,值2,值2...值2 union
.
.
.
select 值n,值n,值n...值n
13 个解决方案
#1
就是union 的方法.
#2
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'
select '1003','周宏伟','男','12' union
select '1004','何小飞','女','43' union
select '1005','李华','女','15' union
select '1006','赵婷','女','31'
#3
建议用union all
#4
就是 union 了,没简单的方法了,除非你的导入表能够
insert into 目标表 select 列1,列2..... from 源表
#5
用union all效率更高一些。除此没有其他更好的了。
#6
--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')
#7
union all
#8
--2000
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')
#9
insert...union all
#10
union all
#11
最简单的方法就是把数据放到EXCEL里面然后是有
INSERT INTO tbl_stu (stu_id,stu_name,stu_sex,stu_age)
SELECT stu_id,stu_name,stu_sex,stu_age FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:test.xls',sheet1$)
在实际工作中我用此方法比较多
INSERT INTO tbl_stu (stu_id,stu_name,stu_sex,stu_age)
SELECT stu_id,stu_name,stu_sex,stu_age FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:test.xls',sheet1$)
在实际工作中我用此方法比较多
#12
up
up
up
#13
INSERT . UNION ALL
#1
就是union 的方法.
#2
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'
select '1003','周宏伟','男','12' union
select '1004','何小飞','女','43' union
select '1005','李华','女','15' union
select '1006','赵婷','女','31'
#3
建议用union all
#4
就是 union 了,没简单的方法了,除非你的导入表能够
insert into 目标表 select 列1,列2..... from 源表
#5
用union all效率更高一些。除此没有其他更好的了。
#6
--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')
#7
union all
#8
--2000
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')
#9
insert...union all
#10
union all
#11
最简单的方法就是把数据放到EXCEL里面然后是有
INSERT INTO tbl_stu (stu_id,stu_name,stu_sex,stu_age)
SELECT stu_id,stu_name,stu_sex,stu_age FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:test.xls',sheet1$)
在实际工作中我用此方法比较多
INSERT INTO tbl_stu (stu_id,stu_name,stu_sex,stu_age)
SELECT stu_id,stu_name,stu_sex,stu_age FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:test.xls',sheet1$)
在实际工作中我用此方法比较多
#12
up
up
up
#13
INSERT . UNION ALL