如何解决 在关键字 'else' 附近有语法错误

我心依旧2014 2010-08-24 01:47:21
drop table rkpc

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)
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 NULL

begin
update rkpc set 户主='是'
where id=@id

else

update rkpc set 户主='否'
where id=@id;
fetch next from cur into @id,@idnum;
end
close cur




服务器: 消息 156,级别 15,状态 1,行 14
在关键字 'else' 附近有语法错误。


这个是怎么回事
...全文
854 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
我心依旧2014 2010-08-24
  • 打赏
  • 举报
回复
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
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


我在第九行加了@就出现这个错误
服务器: 消息 16917,级别 16,状态 2,行 4
游标未打开。

(所影响的行数为 0 行)

服务器: 消息 16917,级别 16,状态 2,行 21
游标未打开。
服务器: 消息 16917,级别 16,状态 1,行 24
游标未打开。



很是郁闷!!还真没有查出来哪儿错了!!

我心依旧2014 2010-08-24
  • 打赏
  • 举报
回复
这个列名怎么会是无效呢,搞不懂了 哪儿语法出现错误
我心依旧2014 2010-08-24
  • 打赏
  • 举报
回复
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
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


这个怎么还有错误啊

服务器: 消息 207,级别 16,状态 3,行 9
列名 'idnum' 无效。


不知道是怎么回事 我怎么查也查不出来哪儿错了

windfeel2008 2010-08-24
  • 打赏
  • 举报
回复
还有

select int,innum from rkpc;
应该是:
select id,idnum from rkpc;

楼主也太粗心了
winstonbonaparte 2010-08-24
  • 打赏
  • 举报
回复
你begin...end没有成对出现,当然就报错了啊
hao1hao2hao3 2010-08-24
  • 打赏
  • 举报
回复

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



要先打开游标才能取数据。


php_boy 2010-08-24
  • 打赏
  • 举报
回复


drop table rkpc
GO

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)
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
feilniu 2010-08-24
  • 打赏
  • 举报
回复
if
begin
end
else
begin
end

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧