drop table rkpccreate 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)
go
select * FROM RKPC
declare @id int,@idnum char(30)
declare cur cursor fast_forward for 
select int,innum from rkpc;
fetch next from cur into @id,@idnum;
open cur
if IDNUM IS NOT NULLbegin
update rkpc set 户主='是'
where id=@idelseupdate rkpc set 户主='否'
where id=@id;
fetch next from cur into @id,@idnum;
end
close cur
服务器: 消息 156,级别 15,状态 1,行 14
在关键字 'else' 附近有语法错误。
这个是怎么回事

解决方案 »

  1.   

    if
    begin
    end
    else
    begin
    end
      

  2.   

    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)
    go
    select * FROM RKPC
    GO
    declare @id int,@idnum char(30)
    declare cur cursor fast_forward for 
    select int,innum from rkpc;
    open cur
    fetch next from cur into @id,@idnum;
    while @@FETCH_STATUS=0
    BEGIN
    if @idnum IS NOT NULL begin
    update rkpc set 户主='是'
    where id=@id
    end
    else
    begin
    update rkpc set 户主='否'
    where id=@id;
    end
    fetch next from cur into @id,@idnum;
    END
    close cur
    deallocate cur
      

  3.   


    declare @id int,@idnum char(30)
    declare cur cursor fast_forward for  
    select int,innum from rkpc;
    open cur
    fetch next from cur into @id,@idnum;
    if IDNUM IS NOT NULL
    begin
    update rkpc set 户主='是'
    where id=@id
    end
    else
    begin
    update rkpc set 户主='否'
    where id=@id;
    end
    fetch next from cur into @id,@idnum;
    close cur要先打开游标才能取数据。
      

  4.   

    你begin...end没有成对出现,当然就报错了啊
      

  5.   

    还有select int,innum  from rkpc;
    应该是:
    select id,idnum  from rkpc;楼主也太粗心了
      

  6.   

    declare @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
    这个怎么还有错误啊服务器: 消息 207,级别 16,状态 3,行 9
    列名 'idnum' 无效。
    不知道是怎么回事  我怎么查也查不出来哪儿错了
      

  7.   

    declare @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
    游标未打开。很是郁闷!!还真没有查出来哪儿错了!!