表中数据的批量插入
--数据的批量插入
if exists(select name from sys.databases where name='DB_Test')
drop database DB_Test
go
create database DB_Test
go
use DB_Test
go
if exists(select name from sys.objects where name='T_Table1')
drop table T_Table1
go
create table T_Table1
(
a nvarchar(10),--姓
b nvarchar(10),--性别
c int,--年龄
constraint PK_T_Table1_a primary key(a)
)
go
insert into T_Table1 values('张','男',20)
insert into T_Table1 values('李','男',22)
insert into T_Table1 values('姚','女',20)
go
select * from T_Table1
go
if exists(select name from sys.objects where name='T_Table2')
drop table T_Table2
go
create table T_Table2
(
d nvarchar(10),
e nvarchar(10),
f int,
constraint PK_T_Table2_d primary key(d)
)
go
--批量插入第一种方式:(目标表一定要存在)
insert into T_Table2(d,e,f) select a,b,c from T_Table1
go
insert into T_Table2(d,e,f) select a,b,20 from T_Table1
go
insert into T_Table2(d,e) select a,b from T_Table1
go
select * from T_Table2
go
--第二种方式:(目标表不存在)
if exists(select name from sys.objects where name='T_Table2')
drop table T_Table2
go
select a,b,c into T_Table2 from T_Table1
go
select a,b into T_Table2 from T_Table1
go
select * from T_Table2
go