游标使用问题,报错DECLARE CURSOR must be the only statement in a query batch

来源:百度知道 编辑:UC知道 时间:2024/06/04 19:31:46
具体程序如下:
DECLARE zm_id cursor for
select pcp_id from pos.physical_count_plan
where pcp_id > 'PD0709120099001'

open zm_id

fetch next from zm_id
while @@fetch status = 0
begin
fetch next from zm_id
end

close zm_id
deallocate zm_id;

要定义一个变量来放游标值啊。
例如:
declare @sid varchar(50)
DECLARE zm_id cursor for
select pcp_id from pos.physical_count_plan
where pcp_id > 'PD0709120099001'

open zm_id

fetch next from zm_id into @sid
while @@fetch status = 0
begin
fetch next from zm_id into @sid
end

close zm_id
deallocate zm_id;