--游标 declare @ss varchar(10) declare testcursor cursor for select ss from tableAA open testcursor fetch first from testcursor into @ss while @@fetch_status = 0 begin if @ss = 'yes' insert into tableBB select * from tableAA where ss = @ss fetch next from testcursor into @ss end close testcursor deallocate testcursor
--例子 declare @id int set @id=0 declare cur_1 cursor for select id from aaopen cur_1 fetch next from cur_1 into @id while @@FETCH_STATUS=0 begin if exists(select id from aa where id=@id and ss='yes') begin insert into bb select 字段 from aa end fetch next from cur_1 into @id endclose cur_1 deallocate cur_1
declare @ss varchar(8) declare @col1 varchar(20),.... declare cursor1 scroll cursor for select col1 , .... from table fetch next from cursor1 into @col1,... while @@fetch_status = 0 begin if @ss = 'yes' begin insert into pb select @col1,@col2,..... end fetch next from cursor1 into @col1 , @col2 ,............ end close cursor1 deallocate cursor1
declare @ss varchar(10)
declare testcursor cursor for select ss from tableAA
open testcursor
fetch first from testcursor into @ss
while @@fetch_status = 0
begin
if @ss = 'yes'
insert into tableBB select * from tableAA where ss = @ss
fetch next from testcursor into @ss
end
close testcursor
deallocate testcursor
declare @id int
set @id=0
declare cur_1 cursor for
select id from aaopen cur_1
fetch next from cur_1 into @id
while @@FETCH_STATUS=0
begin
if exists(select id from aa where id=@id and ss='yes')
begin
insert into bb select 字段 from aa
end
fetch next from cur_1 into @id
endclose cur_1
deallocate cur_1
declare @col1 varchar(20),....
declare cursor1 scroll cursor for select col1 , .... from table
fetch next from cursor1 into @col1,...
while @@fetch_status = 0
begin
if @ss = 'yes'
begin
insert into pb
select @col1,@col2,.....
end
fetch next from cursor1 into @col1 , @col2 ,............
end
close cursor1
deallocate cursor1