读书人

语法异常如何也想不通

发布时间: 2012-05-16 11:12:12 作者: rapoo

语法错误,怎么也想不通
declare cur cursor for...open cur
fetch next from cur into ...
while(@@FETCH_STATUS=0)
if(...)
begin
...
end
fetch next from cur into ...
end

至此没有错误,但是在if后面加一个else段就出错了

declare cur cursor for...open cur
fetch next from cur into ...
while(@@FETCH_STATUS=0)
if(...)
begin
...
end
else
begin
... --即使此处为空也报错
end
fetch next from cur into ...
end

消息 156,级别 15,状态 1,
关键字 'fetch' 附近有语法错误。
消息 102,级别 15,状态 1,
'end' 附近有语法错误。


[解决办法]
while 后面加 begin
[解决办法]

SQL code
--begin end 中必有句(肯定)declare cur cursor for... open curfetch next from cur into ...while(@@FETCH_STATUS=0)begin  if(...)  begin  ...  end  else  begin  ...  end  fetch next from cur into ...end
[解决办法]
SQL code
declare cur cursor for... open curfetch next from cur into ...while(@@FETCH_STATUS=0)    begin          if(...)          begin          ...          end          else          begin          ... --即使此处为空也报错          end    fetch next from cur into ...    end    close cur    deallocate cur
[解决办法]
while(@@FETCH_STATUS=0)
begin if(...)
begin
...
end
else
begin
... --即使此处为空也报错
end
fetch next from cur into ...
end


[解决办法]
语法没什么问题,看下是不是有中文的空格
[解决办法]
SQL code
IF OBJECT_ID('student') is not nulldrop table studentgo create table student(id int ,name varchar(10),gender int check(gender=1 or gender=2))go insert into student values(1001,'tracy',1)insert into student values(1002,'lily',2)insert into student values(1003,'kobe',1)insert into student values(1004,'lucy',2)insert into student values(1005,'nash',1)godeclare cur cursor for select *from studentdeclare @id int,@name varchar(10),@gender intopen curfetch next from cur into @id,@name,@genderwhile @@fetch_status=0begin if @id=1001 or @id=1002 begin  print ltrim(str(@id))+','+@name+','+ltrim(str(@gender))  fetch next from cur into @id,@name,@gender  print' hello' end else  if  @id=1003 begin   print ltrim(str(@id))+','+@name+','+ltrim(str(@gender))  fetch next from cur into @id,@name,@gender  print' nihao' end else begin   print ltrim(str(@id))+','+@name+','+ltrim(str(@gender))  fetch next from cur into @id,@name,@gender  print '睡觉了' endend    close cur  deallocate cur    /*  1001,tracy,1 hello1002,lily,2 hello1003,kobe,1 nihao1004,lucy,2睡觉了1005,nash,1睡觉了  */看我的代码
[解决办法]
完整代码贴出
[解决办法]
探讨

中文空格也检查了,现在的情况,把if段begin end内的代码去掉还是这个错误;如果只加else,不报错,else后面加begin end就出错。
郁闷啊~~~~

[解决办法]
第一个if后面你用的是中文的括号
[解决办法]
if @dptname='全部机构' 后面的Begin...end错了
[解决办法]
SQL code
ALTER procedure [dbo].[sp_count_dpt]@dptname nvarchar(50),  @dptdate1 nvarchar(50),  @dptdate2 nvarchar(50)as  declare@dpttemp nvarchar(50),@dpt1temp nvarchar(50),@tempct int  delete from dptcount declare cur cursor for( select dpt,dpt1 from dpt1 where dpt=@dptname)--用游标取得所有机构关键词open curfetch next from cur into @dpttemp,@dpt1tempwhile @@FETCH_STATUS=0  begin  if @dptname='全部机构'  beginset @tempct =(select COUNT(*) from thread where threadtitle like '%'+@dpt1temp+'%' andthreaddate>=CAST(@dptdate1 as datetime) and threaddate<=CAST(@dptdate2 as datetime))insert into dptcount(dpt,dpt1,dptcount) values(@dpttemp,@dpt1temp,@tempct)  fetch next from cur into @dpttemp,@dpt1tempend    elsebegin--这里必须写代码endend close cur   deallocate cur 

读书人网 >SQL Server

热点推荐