准备工作
创建两个数据结构相同的表t1和t2,并插入数据到t1
create table t1 ( id int identity (1,1) primary key not null, iname nvarchar(20) ) create table t2 ( id int identity (1,1) primary key not null, iname nvarchar(20) ) insert into t1 (iname) values (‘a‘) insert into t1 (iname) values (‘a2‘) insert into t1 (iname) values (‘a3‘) insert into t1 (iname) values (‘a4‘) insert into t1 (iname) values (‘a5‘) insert into t1 (iname) values (‘a6‘) insert into t1 (iname) values (‘a7‘)
(1)之前不存在t3表,创建t3表并将t1中的数据复制到t3中
select * into t3 from t1
(2)t2表的主键需要打开
set IDENTITY_INSERT t2 on INSERT INTO t2 ( id,iname) SELECT * FROM t1 set IDENTITY_INSERT t2 off
时间: 2024-11-06 23:42:13