一般出現這樣的問題是:
1:游標沒有 --關閉 釋放
如:
- --關閉游標
- CLOSE MM_CURSOR
- --釋放游標
- DEALLOCATE MM_CURSOR
2:游標已存在同名情況,此時就需要在定義游標時申明一個局部的游標
如:
- /*檢索已經配置好的新村鎮的所有鄉級部門*/
- ---申明游標
- DECLARE deptCursor CURSOR
- local FOR
- SELECT deptname, deptsimplename,distid, deptuncode,deptqueryno,ifreport,deptsort,enable,deptfloor,deptcharacter,caseSMSFlag,deptType
- FROM t_department
- where PARENTID=250 and deptType='2'
其實我的情況都不是這樣,只是在使用嵌套多層循環操作時把兩個游標全部放在存儲過程末后
- --關閉游標
- CLOSE MM_CURSOR
- --釋放游標
- DEALLOCATE MM_CURSOR
- --關閉游標--釋放游標
- CLOSE deptCursor
- --釋放游標
- DEALLOCATE deptCursor
沒有及時關閉導致問題出現!
正確代碼如下
- set ANSI_NULLS ON
- set QUOTED_IDENTIFIER ON
- go
- ---drop PROCEDURE copyDept
- ALTER PROCEDURE [dbo].[copyDept]
- as
- declare @deptCode varchar(20)
- declare @deptname varchar(10)
- declare @deptsimplename varchar(100)
- declare @distid bigint
- declare @deptuncode varchar(100)
- declare @deptqueryno varchar(100)
- declare @ifreport varchar(4)
- declare @deptsort int
- declare @enable varchar(6)
- declare @deptfloor smallint
- declare @deptcharacter varchar(50)
- declare @caseSMSFlag varchar(4)
- declare @deptType varchar(1)
- declare @DeNo bigint
- set nocount on
- begin
- set @deptcode = '2000000'
- /*檢索已經配置好的新村鎮的所有鄉級部門*/
- ---申明游標
- DECLARE deptCursor CURSOR
- local FOR
- SELECT deptname, deptsimplename,distid, deptuncode,deptqueryno,ifreport,deptsort,enable,deptfloor,deptcharacter,caseSMSFlag,deptType
- FROM t_department
- where PARENTID=250 and deptType='2'
- ---打開游標
- OPEN deptCursor
- --循環取出游標
- FETCH NEXT FROM deptCursor INTO @deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@deptcharacter,@caseSMSFlag,@deptType
- while (@@FETCH_STATUS = 0)
- begin
- /*檢索鄉鎮行政部門:如趙集鎮,龍王鄉...*/
- ---申明游標
- Declare MM_CURSOR CURSOR
- local FOR
- 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
- Order by DEPTCODE /**ONLY VALID DEPARTMENT */
- -- 打開游標
- open MM_CURSOR
- --循環取出游標
- FETCH NEXT FROM MM_CURSOR INTO @DeNo
- while (@@FETCH_STATUS = 0)
- BEGIN
- set @deptcode = convert(varchar(20),cast(@deptcode as int)+1)
- print(@deptcode)
- INSERT INTO T_DEPARTMENT (deptcode, deptname, deptsimplename,distid, deptuncode,deptqueryno,ifreport,deptsort,enable,deptfloor,PARENTID,deptcharacter,caseSMSFlag,deptType)
- VALUES (@deptcode,@deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@DeNo,@deptcharacter,@caseSMSFlag,@deptType)
- FETCH NEXT FROM MM_CURSOR INTO @DeNo
- END
- --關閉游標
- CLOSE MM_CURSOR
- --釋放游標
- DEALLOCATE MM_CURSOR
- FETCH NEXT FROM deptCursor INTO @deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@deptcharacter,@caseSMSFlag,@deptType
- --@deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@deptcharacter,@caseSMSFlag,@deptType
- end
- end
- --關閉游標
- CLOSE deptCursor
- --釋放游標
- DEALLOCATE deptCursor
此外,在剛開始調用存儲過程還遇到一個問題:程序處於正在查詢狀態,近一個小時,我想,數據還沒那么復雜,可能出現死循環或某個游標沒有移動...
可是看了代碼,沒有出現這樣的情況,
經同事指點:
- ---申明游標
- Declare MM_CURSOR CURSOR
- local FOR
- 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
- Order by DEPTCODE /**ONLY VALID DEPARTMENT */
- -- 打開游標
- open MM_CURSOR
- --循環取出游標
- FETCH NEXT FROM MM_CURSOR INTO @DeNo
- while (@@FETCH_STATUS = 0)
- set @deptcode = convert(varchar(20),cast(@deptcode as int)+1) //把此行代碼移至begin代碼內即可
- BEGIN
- print(@deptcode)
- INSERT INTO T_DEPARTMENT (deptcode, deptname, deptsimplename,distid, deptuncode,deptqueryno,ifreport,deptsort,enable,deptfloor,PARENTID,deptcharacter,caseSMSFlag,deptType)
- VALUES (@deptcode,@deptname,@deptsimplename,@distid,@deptuncode,@deptqueryno,@ifreport,@deptsort,@enable,@deptfloor,@DeNo,@deptcharacter,@caseSMSFlag,@deptType)
- FETCH NEXT FROM MM_CURSOR INTO @DeNo
- END
- --關閉游標
- CLOSE MM_CURSOR
- --釋放游標
- DEALLOCATE MM_CURSOR