declare customer_cur scroll cursor
for select * from customers
open customer_cur
print @@cursor_rows
declare @CompanyName nvarchar(40)
fetch next from customer_cur into @CompanyName --这里我只想用其中的一列,可以这样用吗?
while @@fetch_status=0
begin
print @companyname
    fetch next from customer_cur into @CompanyName
end我想在游标里取出其中的一列,在上面已经写明我的目的,请问这样可以吗?

解决方案 »

  1.   


    declare customer_cur scroll cursor 
    for select CompanyNamefrom customers 
    open customer_cur 
    print @@cursor_rows 
    declare @CompanyName nvarchar(40) 
    fetch next from customer_cur into @CompanyName --这里我只想用其中的一列,可以这样用吗? 
    while @@fetch_status=0 
    begin 
        print @companyname 
        fetch next from customer_cur into @CompanyName 
    end 
      

  2.   

    declare customer_cur scroll cursor 
    for select CompanyName from customers  --列的列表和你的fetch对应
    open customer_cur 
    print @@cursor_rows 
    declare @CompanyName nvarchar(40) 
    fetch next from customer_cur into @CompanyName --这里我只想用其中的一列,可以这样用吗? 
    while @@fetch_status=0 
    begin 
        print @companyname 
        fetch next from customer_cur into @CompanyName 
    end 
      

  3.   

    declare customer_cur scroll cursor 
    --这里改一下,指定你要的列名******************
    for select colname from customers 
    --***********************************
    open customer_cur 
    print @@cursor_rows 
    declare @CompanyName nvarchar(40) 
    fetch next from customer_cur into @CompanyName --这里我只想用其中的一列,可以这样用吗? 
    while @@fetch_status=0 
    begin 
    print @companyname 
        fetch next from customer_cur into @CompanyName 
    end 
      

  4.   

    --这里我只想用其中的一列,可以这样用吗?那你定义cursor的时候,也select那一列
      

  5.   

    那就是用什么就取什么,不能像select那样自由再选择所需的列?
      

  6.   

    对,你同时选了好几列出来,SQL 怎么知道把哪列给变量呢?