一个表中有很多字段(比如50个字段),查询后有一条记录符合条件,
一般在查询中用 select @变量1= 字段1, @变量2= 字段2, @变量2= 字段2 …… FROM 表名 
是不是只能用列举的方法把这么多变量与字段对应起来?
我想用一其它方法:比如循环的方法把每个字段中的值符值给变量,谢谢各位提示一下?

解决方案 »

  1.   

    --游标用法
    if object_id('tb') is not null
    drop table tb
    go
    create table tb(name varchar(10),con int)
    insert into tb select 'a',1
    insert into tb select 'b',2
    insert into tb select 'c',3declare cursor_tb cursor for
    select name ,con FROM tb
    declare @name varchar(10),@con int
    open cursor_tb
    fetch next from cursor_tb
    into @name, @con
    while @@FETCH_STATUS = 0
    begin
    select @name,@con--这里操作
    fetch next from cursor_tb
    into @name, @con
    end
    close cursor_tb
    deallocate cursor_tb
      

  2.   

    假如有规律的话,可以写个动态的SQL