日期:2014-05-18 浏览次数:20638 次
--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
------解决方案--------------------
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
------解决方案--------------------
语法没什么问题,看下是不是有中文的空格
------解决方案--------------------
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
睡觉了
*/
看我的代码
------解决方案--------------------
完整代码贴出
------解决方案--------------------
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