解决为'*********' 的游标已存在问题

出现名为'MM_CURSOR' 的游标已存在。 
一般出现这样的问题是: 
1:游标没有    --关闭 释放 
如: 
Sql代码  
  1. --关闭游标  
  2.       CLOSE MM_CURSOR  
  3.         --释放游标  
  4.      DEALLOCATE MM_CURSOR  


2:游标已存在同名情况,此时就需要在定义游标时申明一个局部的游标 
如: 
Sql代码  
  1. /*检索已经配置好的新村镇的所有乡级部门*/  
  2.    ---申明游标  
  3. DECLARE deptCursor CURSOR   
  4.    local FOR   
  5.    SELECT  deptname, deptsimplename,distid, deptuncode,deptqueryno,ifreport,deptsort,enable,deptfloor,deptcharacter,caseSMSFlag,deptType  
  6.            
  7.    FROM t_department   
  8.    where  PARENTID=250 and deptType='2'  




其实我的情况都不是这样,只是在使用嵌套多层循环操作时把两个游标全部放在存储过程末后 

Sql代码  
  1. --关闭游标  
  2.       CLOSE MM_CURSOR  
  3.         --释放游标  
  4.      DEALLOCATE MM_CURSOR  
  5.    --关闭游标--释放游标  
  6. CLOSE deptCursor  
  7.    --释放游标  
  8. DEALLOCATE deptCursor  

没有及时关闭导致问题出现! 
正确代码如下 
Sql代码  
  1. set ANSI_NULLS ON  
  2. set QUOTED_IDENTIFIER ON  
  3. go  
  4. ---drop  PROCEDURE copyDept  
  5.   
  6. ALTER PROCEDURE [dbo].[copyDept]  
  7.     as  
  8.     declare @deptCode varchar(20)  
  9.     declare @deptname varchar(10)  
  10.     declare @deptsimplename varchar(100)  
  11.     declare @distid bigint  
  12.     declare @deptuncode varchar(100)  
  13.     declare @deptqueryno varchar(100)  
  14.     declare @ifreport varchar(4)  
  15.     declare @deptsort int  
  16.     declare @enable varchar(6)  
  17.     declare @deptfloor smallint  
  18.     declare @deptcharacter varchar(50)  
  19.     declare @caseSMSFlag varchar(4)  
  20.     declare @deptType varchar(1)  
  21.     declare @DeNo bigint  
  22.     set nocount on  
  23.     begin  
  24.     set  @deptcode = '2000000'  
  25.     /*检索已经配置好的新村镇的所有乡级部门*/  
  26.     ---申明游标  
  27.     DECLARE deptCursor CURSOR   
  28.     local FOR   
  29.     SELECT  deptname, deptsimplename,distid, deptuncode,deptqueryno,ifreport,deptsort,enable,deptfloor,deptcharacter,caseSMSFlag,deptType  
  30.                
  31.     FROM t_department   
  32.     where  PARENTID=250 and deptType='2'  
  33.     ---打开游标  
  34.     OPEN deptCursor  
  35.     --循环取出游标      
  36.     FETCH NEXT FROM deptCursor INTO @deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@deptcharacter,@caseSMSFlag,@deptType     
  37.     while (@@FETCH_STATUS = 0)  
  38.         begin  
  39.             /*检索乡镇行政部门:如赵集镇,龙王乡...*/  
  40.             ---申明游标  
  41.             Declare MM_CURSOR CURSOR  
  42.             local  FOR  
  43.             Select DEPTID from t_department where  ENABLE= '启用' and DISTID = 1 and deptType=0 and deptid !=250---demo,except 250 -- and PARENTID =288--and deptid not in (243,244)--and is_convenience=@tjType jc_dreaming  
  44.             Order by DEPTCODE /**ONLY VALID DEPARTMENT */  
  45.             -- 打开游标  
  46.             open MM_CURSOR  
  47.             --循环取出游标      
  48.             FETCH NEXT FROM MM_CURSOR  INTO @DeNo  
  49.             while (@@FETCH_STATUS = 0)                
  50.                 BEGIN  
  51.                  set @deptcode = convert(varchar(20),cast(@deptcode as int)+1)  
  52.                 print(@deptcode)  
  53.                  INSERT INTO T_DEPARTMENT (deptcode, deptname, deptsimplename,distid, deptuncode,deptqueryno,ifreport,deptsort,enable,deptfloor,PARENTID,deptcharacter,caseSMSFlag,deptType)  
  54.                                     VALUES (@deptcode,@deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@DeNo,@deptcharacter,@caseSMSFlag,@deptType)  
  55.                 FETCH NEXT FROM MM_CURSOR INTO @DeNo  
  56.                 END  
  57.             --关闭游标  
  58.           CLOSE MM_CURSOR  
  59.          --释放游标  
  60.          DEALLOCATE MM_CURSOR  
  61.         FETCH NEXT FROM deptCursor INTO @deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@deptcharacter,@caseSMSFlag,@deptType     
  62.                                         --@deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@deptcharacter,@caseSMSFlag,@deptType  
  63.         end  
  64.           
  65.     end  
  66.    
  67.     --关闭游标  
  68.     CLOSE deptCursor  
  69.     --释放游标  
  70.     DEALLOCATE deptCursor  



此外,在刚开始调用存储过程还遇到一个问题:程序处于正在查询状态,近一个小时,我想,数据还没那么复杂,可能出现死循环或某个游标没有移动... 
可是看了代码,没有出现这样的情况, 
经同事指点: 
Sql代码  
  1. ---申明游标  
  2.             Declare MM_CURSOR CURSOR  
  3.             local  FOR  
  4.             Select DEPTID from t_department where  ENABLE= '启用' and DISTID = 1 and deptType=0 and deptid !=250---demo,except 250 -- and PARENTID =288--and deptid not in (243,244)--and is_convenience=@tjType jc_dreaming  
  5.             Order by DEPTCODE /**ONLY VALID DEPARTMENT */  
  6.             -- 打开游标  
  7.             open MM_CURSOR  
  8.             --循环取出游标      
  9.             FETCH NEXT FROM MM_CURSOR  INTO @DeNo  
  10.             while (@@FETCH_STATUS = 0)    
  11.                 set @deptcode = convert(varchar(20),cast(@deptcode as int)+1)   //把此行代码移至begin代码内即可       
  12.                 BEGIN  
  13.                  
  14.                 print(@deptcode)  
  15.                  INSERT INTO T_DEPARTMENT (deptcode, deptname, deptsimplename,distid, deptuncode,deptqueryno,ifreport,deptsort,enable,deptfloor,PARENTID,deptcharacter,caseSMSFlag,deptType)  
  16.                                     VALUES (@deptcode,@deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@DeNo,@deptcharacter,@caseSMSFlag,@deptType)  
  17.                 FETCH NEXT FROM MM_CURSOR INTO @DeNo  
  18.                 END  
  19.             --关闭游标  
  20.           CLOSE MM_CURSOR  
  21.          --释放游标  
  22.          DEALLOCATE MM_CURSOR
posted @ 2012-07-02 17:55  挑战  阅读(9228)  评论(1编辑  收藏  举报