在存储过程中 报 名为 'mycursor' 的游标已存在
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[jzl_getFrequentChangeStatus]
@area int,
@mjno varchar(20)
as
declare @cnt int,@index int, @sta_id varchar(50),@sta_id1 varchar(50),@pos_id varchar(50),@pos_id1 varchar(10),@begin_time datetime
declare @taskno varchar(20)
IF EXISTS (SELECT * from [tempdb].dbo.sysobjects WHERE id = object_id(N'[tempdb].[dbo].#mytmp') )
DROP Table #mytmp
CREATE TABLE #mytmp(
[Sta_id] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[Begin_time] [datetime] NULL ,
[Pos_id] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[Bcch_0] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[taskNo] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[sta_addr] [varchar] (150) COLLATE Chinese_PRC_CI_AS NULL ,
[pos_addr] [varchar] (150) COLLATE Chinese_PRC_CI_AS NULL ,
)
if @area=-1 and @mjno='all'
begin
--print '1'
declare mycursor scroll cursor for
select sta_id,pos_id,begin_time,taskno from test_bcchmsg order by rtrim(sta_id)+rtrim(pos_id),begin_time desc
select @cnt = count(*) from(select * from test_bcchmsg) a
end
else if @area>-1 and @mjno='all'
begin
declare mycursor scroll cursor for
select a.sta_id,a.pos_id,a.begin_time,a.taskno from test_bcchmsg a inner join share_setting b on(a.sta_id=b.sta_id)
where b.area = @area
order by rtrim(a.sta_id)+rtrim(a.pos_id),a.begin_time desc
select @cnt = count(*) from
(select a.* from bcch_testmsg a inner join share_setting b on(a.sta_id=b.sta_id)
where b.area = @area) a
end
else if @mjno<>'all'
begin
declare mycursor scroll cursor for
select a.sta_id,a.pos_id,a.begin_time, a.taskno from test_bcchmsg a inner join share_setting b on(a.sta_id=b.sta_id)
where a.sta_id=@mjno
order by rtrim(a.sta_id)+rtrim(a.pos_id),a.begin_time desc
select @cnt = count(*) from
(select a.* from bcch_testmsg a inner join share_setting b on(a.sta_id=b.sta_id)
where a.sta_id=@mjno) a
end
open mycursor
if ( @@CURSOR_ROWS > 0 )
begin
set @index = 1
set @sta_id1 = ''
set @pos_id1 = ''
FETCH first FROM mycursor into @sta_id,@pos_id,@begin_time,@taskno
WHILE @@FETCH_STATUS = 0
BEGIN
--if @sta_id1<>@sta_id or @pos_id1 <> @pos_id
begin
set @sta_id1 = @sta_id
set @pos_id1 = @pos_id
insert into #mytmp
select a.sta_id,a.begin_time,a.b_count,a.fretime,a.pos_id,a.bcch,
a.taskno,b.main_addr,f.addr
from test_bcchmsg a inner join share_setting b on(a.sta_id=b.sta_id)
inner join child_setting f on( a.sta_id=f.sta_id and a.pos_id=f.pos_id )
where a.sta_id=@sta_id and a.pos_id=@pos_id and begin_time=@begin_time and taskno=@taskno
set @index = @index+1
if @index >@cnt break
end
FETCH next from mycursor into @sta_id,@pos_id,@begin_time,@taskno
end
end
close mycursor
deallocate mycursor
select * from #mytmp
DROP Table #mytmp
------解决方案--------------------
SQL code
--> 看错,没有嵌套游标,改为本地游标,不然存储过程并发就会出现这个问题:
declare @mycursor cursor
set @mycursor = cursor scroll for
select sta_id,pos_id,begin_time,taskno from test_bcchmsg order by rtrim(sta_id)+rtrim(pos_id),begin_time desc
--> 其他自己改改。