4

如何重写以下 SP 以面对 MySQL 中的递归限制:

CREATE DEFINER = `root`@`localhost` PROCEDURE `SP_DeleteParentDirectory`(pParentID INT,pIsFolder INT,pReferenceID INT)
BEGIN   
SET @@SESSION.max_sp_recursion_depth = 255;
    SET @IsFolder= NULL;
    SET @ChildID= NULL;
    SET @ReferenceID= NULL;
    IF ( pIsFolder= 2) THEN
                SELECT  FileID INTO @ChildID FROM filesinfo WHERE ParentID= pParentID LIMIT 1;
                WHILE (@ChildID IS NOT NULL) DO
                         SELECT  filesinfo.IsFolder,filesinfo.ReferenceID INTO @IsFolder,@ReferenceID FROM filesinfo WHERE filesinfo.FileID = @ChildID;
                        CALL SP_DeleteParentDirectory(@ChildID,@IsFolder,@ReferenceID);             
                        SELECT filesinfo.FileID INTO @ChildID FROM filesinfo WHERE ParentID= pParentID LIMIT 1;
                END While;
    END IF;
    DELETE FROM filesinfo Where  filesinfo.FileID= pParentID;
    IF EXISTS( SELECT  * FROM  filesrefrences WHERE filesrefrences.ReferenceID = pReferenceID AND filesrefrences.RefrenceCount>1) THEN
            UPDATE filesrefrences
            SET RefrenceCount= RefrenceCount-1 WHERE  filesrefrences.ReferenceID = pReferenceID;            
    ELSE            
            SET @ReferenceKey= NULL;
            Select filesrefrences.RefrenceKey INTO @ReferenceKey FROM filesrefrences WHERE filesrefrences.ReferenceID= pReferenceID;
            IF NOT EXISTS(SELECT * FROM deletefilesreferences WHERE deletefilesreferences.FileReference= @ReferenceKey) THEN
            INSERT INTO deletefilesreferences (deletefilesreferences.FileReference)VALUES(@ReferenceKey);
            END IF;
            DELETE FROM filesrefrences where filesrefrences.ReferenceID = pReferenceID;         
    END IF;
    -- CALL SP_DeleteFileInfo(pParentID,pReferenceID);      
    -- SELECT * FROM deletefilesreferences;
    -- CALL SP_GetDeletedReferences();
END;

不使用这个 sts :

" SET @@SESSION.max_sp_recursion_depth = 255; " 我的意思是可以不递归地与层次数据交互。

4

1 回答 1

1

如果设置 max_sp_recursion_depth,Mysql 最多允许在 PROCEDURE 中进行 255 次递归。

这是有风险的,因为未来某个时候所需的递归深度可能会超过 255。

所以最好用一个过程替换你的函数,使用一个INOUT变量作为return_result。

于 2013-01-14T11:08:42.120 回答