解決為'*********' 的游標已存在問題


出現名為'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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM