drop table rkpc
GOcreate table rkpc(id int, name varchar(10), idnum int,户主 varchar(10))
insert into rkpc values(1 , '张三' , 434300 ,null)
insert into rkpc values(2 , '张三老婆', null ,null)
insert into rkpc values(3 , '张三女儿', null ,null)
insert into rkpc values(4 , '李四' , 434311 ,null)
insert into rkpc values(5 , '李四老婆', null ,null)
insert into rkpc values(6 , '李四儿子', null ,null)
godeclare @id int,@idnum char(30)
declare cur cursor fast_forward for  
select id,idnum from rkpc;
fetch next from cur into @id,@idnum
while @@FETCH_STATUS=0
open cur
beginif @idnum IS NOT NULLbegin
update rkpc set 户主='是'
where id=@id
end
elsebegin
update rkpc set 户主='否'
where id=@id;
end
fetch next from cur into @id,@idnum;
endclose cur
deallocate cur
我在第九行加了@就出现这个错误
服务器: 消息 16917,级别 16,状态 2,行 4
游标未打开。(所影响的行数为 0 行)服务器: 消息 16917,级别 16,状态 2,行 21
游标未打开。
服务器: 消息 16917,级别 16,状态 1,行 24
游标未打开。第九行不加@,出现这个错误服务器: 消息 207,级别 16,状态 3,行 9
列名 'idnum' 无效。
不知道是怎么回事 我怎么查也查不出来哪儿错了很是郁闷!!还真没有查出来哪儿错了!!

解决方案 »

  1.   

    open cur
    放到fetch前面
      

  2.   


    open cur
    fetch next from cur into @id,@idnum
    while @@FETCH_STATUS=0beginif @idnum IS NOT NULLbegin
    update rkpc set 户主='是'
    where id=@id
    end
    elsebegin
    update rkpc set 户主='否'
    where id=@id;
    end
    fetch next from cur into @id,@idnum;
    endclose cur
    deallocate cur
      

  3.   

    create table rkpc(id int, name varchar(10), idnum int,户主 varchar(10))
    insert into rkpc values(1 , '张三' , 434300 ,null)
    insert into rkpc values(2 , '张三老婆', null ,null)
    insert into rkpc values(3 , '张三女儿', null ,null)
    insert into rkpc values(4 , '李四' , 434311 ,null)
    insert into rkpc values(5 , '李四老婆', null ,null)
    insert into rkpc values(6 , '李四儿子', null ,null)
    declare @id int,@idnum char(30)
    declare cur cursor fast_forward for   
    select id,idnum from rkpc;
    open cur
    fetch next from cur into @id,@idnum
    while @@FETCH_STATUS=0 
    beginif @idnum IS NOT NULLbegin
    update rkpc set 户主='是'
    where id=@id
    end
    elsebegin
    update rkpc set 户主='否'
    where id=@id;
    end
    fetch next from cur into @id,@idnum;
    endclose cur
    deallocate cur
      

  4.   

    这样可以不?
    update rkpc set 户主 case when  idnum IS NOT NULL then '是'
     else '否' end
      

  5.   

    fetch next from cur into @id,@idnum
    while @@FETCH_STATUS=0
    open cur调整为:
    open cur  ---先打开游标才能读取
    fetch next from cur into @id,@idnum
    while @@FETCH_STATUS=0