我想通过一个循环语句,来逐条检查一个表中的记录,假如这个表中有个字段名是ss,而其值是yes或者no ,我想逐条循环检查,如果是yes 我就把该记录信息插入到另一个表bb中,就是不知道这个循环语句怎么写啊?请大家帮帮忙吧,最好是写详细点了,我还是初学者呢,谢谢了

解决方案 »

  1.   

    select 需要的字典 into bb from aa where ss='yes'
      

  2.   

    --游标
    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
      

  3.   

    --例子
    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
      

  4.   

    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