日期:2014-05-18  浏览次数:20526 次

语法错误,怎么也想不通
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 cur
fetch 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 cur
fetch 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 null
drop table student
go 
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)
go

declare cur cursor for select *from student
declare @id int,@name varchar(10),@gender int
open cur
fetch next from cur into @id,@name,@gender
while @@fetch_status=0
begin
 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 '睡觉了'
 end
end  
  close cur
  deallocate cur
  
  /*
  1001,tracy,1
 hello
1002,lily,2
 hello
1003,kobe,1
 nihao
1004,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 cur
fetch next from cur in