我是用游标来实现的,不知还有没有其它的方法declare cursor1 cursor for
select * from table1
open dd
declare @id int,@aa int,@bb int
fetch next from dd into @id, @aa,@bb
begin
if exists (select name from sysobjects where name='table2')
drop table table2
set @id=(select min(id) from table1)-1
while @id<(select max(id) from table1)
begin
set @id=@id+1
set rowcount 1
if exists (select name from sysobjects where name='table2')
insert into table2 select * from table1 where id=@id
else
select * into table2 from table1 where id=@id
end
end
set rowcount 0
select * from table2
select * from table1
open dd
declare @id int,@aa int,@bb int
fetch next from dd into @id, @aa,@bb
begin
if exists (select name from sysobjects where name='table2')
drop table table2
set @id=(select min(id) from table1)-1
while @id<(select max(id) from table1)
begin
set @id=@id+1
set rowcount 1
if exists (select name from sysobjects where name='table2')
insert into table2 select * from table1 where id=@id
else
select * into table2 from table1 where id=@id
end
end
set rowcount 0
select * from table2
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货