一次性插入多条记录
一、联合结果集union(集合运算符)的使用方法
1、集合运算符是对两个集合的操作, 两个集合必须有相同的列数,列具有相同的数据类型(至少能够进行隐式类型转换),最终输出集合的列名由第一个集合的列名确定。
select 100,100
union
select 100,200
2、union,默认去除重复,相对默认使用了distinct;要想全部显示,使用union all
select 100,100
union
select 100,100
select 100,100
union all
select 100,100
3、使用union一次插入多条记录,当插入的数据相同时,不会重复插入,除非使用了union all;当插入的语句中存在union和union all时,还是排除重复数据插入
insert into admin
select 'abc','abc' union
select 'sfas','fasd'
insert into admin
select 'abc','abc' union
select 'abc','abc'
insert into admin
select 'abc','abc' union
select 'abc','abc' union all
select 'sfas','fasd'
3、把现有表的数据插入到新表(表不能存在),为表创建备份;通过这种方式只会复制表中的数据,以及列的名字和数据类型,对于约束,不会复制过来
- 为student备份数据
select * into newStudent from student --newStudent在查询的同时自动创建
- 复制表结构
select top 0 * into newStudent from student --效率高
- 把现有表中的数据插入到一个已存在的表中
insert into newStudent select * from student