分类: WINDOWS
2008-10-16 11:37:27
昨天在学习存储过程的时候,由于使用了递归,出现了游标冲突的问题。经过今天一天的努力,终于解决了。现在将程序贴在这里,供大家参考。
DesignBOMDetail表结构如下图:
-- =============================================
-- Author: 徐昌煜
-- Create date: 2006-9-16
-- Description: 物料非法递归检查,调用了P_USE_ISSUBPARTRECURSION_XU
-- =============================================
ALTER PROCEDURE dbo.P_ISSUBPARTRECURSION_XU
@MAINPARTID VARCHAR(20)='',@SUBPARTID VARCHAR(20)='',@ISRECURSION BIT OUTPUT
AS
BEGIN
DECLARE @SUBPARTID_TEMP VARCHAR(20),@RECORDNUM INT,@ID INT
IF @MAINPARTID=@SUBPARTID
BEGIN
SET @ISRECURSION=1
RETURN 1
END
ELSE
BEGIN
SELECT * FROM DesignBOMDetail WHERE
SELECT @RECORDNUM=@@ROWCOUNT
IF @RECORDNUM=0
BEGIN
SET @ISRECURSION=0
RETURN 0
END
ELSE
BEGIN
DECLARE @MAIN_CUR CURSOR
EXECUTE P_USE_ISSUBPARTRECURSION_XU @SUBPARTID,@MAIN_CUR OUTPUT
FETCH NEXT FROM @MAIN_CUR INTO @ID
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @SUBPARTID_TEMP=SubPartID FROM DesignBOMDetail WHERE
IF @SUBPARTID_TEMP=@MAINPARTID
BEGIN
SET @ISRECURSION=1
RETURN 1
END
ELSE
BEGIN
EXECUTE P_ISSUBPARTRECURSION_XU @MAINPARTID,@SUBPARTID_TEMP,@ISRECURSION
IF @ISRECURSION=1
BEGIN
RETURN 1
END
END
FETCH NEXT FROM @MAIN_CUR INTO @ID
END
END
END
SET @ISRECURSION=0
RETURN 0
END
-- =============================================
-- Author: 徐昌煜
-- Create date: 2006-9-16
-- Description: 使用P_ISSUBPARTRECURSION_XU时,生成游标。
-- =============================================
ALTER PROCEDURE dbo.P_USE_ISSUBPARTRECURSION_XU
@SUBPARTID VARCHAR(20)='',@TEMP_CURSOR CURSOR VARYING OUTPUT
AS
BEGIN
SET @TEMP_CURSOR=CURSOR LOCAL FOR SELECT ID FROM DesignBOMDetail WHERE
OPEN @TEMP_CURSOR
END
/* SET NOCOUNT ON */
RETURN