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))+',[email protected]+','+ltrim(str(@gender)) fetch next from cur into @id,@name,@gender print' hello' end else if @id=1003 begin print ltrim(str(@id))+',[email protected]+','+ltrim(str(@gender)) fetch next from cur into @id,@name,@gender print' nihao' end else begin print ltrim(str(@id))+',[email protected]+','+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后面你用的是中文的括号
------解决方案--------------------
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 [email protected])--用游标取得所有机构关键词open curfetch next from cur into @dpttemp,@dpt1tempwhile @@FETCH_STATUS=0 begin if @dptname='全部机构' beginset @tempct =(select COUNT(*) from thread where threadtitle like [email protected]+'%' 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