如题. 
例如: 
 DECLARE @CurVar CURSOR
 SET @CurVar = CURSOR FOR SELECT id, name FROM embloyee OPEN @CurVar
 FETCH NEXT FORM @CurVar
 ....我如何分别取得当前行的ID和Name字段的值..我都看了一整上午的SQL Server联机丛书, 都没有看到介绍.

解决方案 »

  1.   

    SQL游标用法
    游标:是用来对表从上下每行循环取值,将值连接成为字符串.
    例子:
    对 pubs 数据库的dbo.titles 表。
    1.取得表中的总价格:select sum(price) from dbo.titles
    2.但是我想得到这样一个结果:书名,价格。
    精通ASP,39元;学习vc++,28元;JAVA编程,23元
    则用到游标:声明游标:
    declare titprice CURSOR FAST_FORWARD for
    select title, price from dbo.titles where price<15 打开游标:
    open titprice循环取质
    fetch next from titprice into @strtitle, @strprice
    while @@fetch_status=0
    begin
       if @str=''
       set @str=@strtitle+':   '+Convert(varchar(20),@strprice) 
    else
        set @str=@str+@strtitle+':   '+Convert(varchar(20),@strprice)   fetch next from titprice into @strtitle,@strprice
    end关闭游标
    close titprice
    释放游标
    DEALLOCATE titprice
    print @str
      

  2.   


    declare @id int,@name varchar(100)
    DECLARE CurVar  CURSOR FOR SELECT id, name FROM embloyee 
    OPEN CurVar 
    FETCH NEXT from CurVar into @id,@name
    while(@@fetch_status=0)
    begin
    select '当前id:'+ltrim(@id)+';当前name:'+@name
    FETCH NEXT from CurVar into @id,@name
    end
    close curvar
    deallocate curvar