SQL删除树形结构

全部查询到临时表中 然后 批量删除
ALTER PROCEDURE [dbo].[proc_DocDelFolder] @FolderId INT
AS 
    BEGIN
        SET NOCOUNT OFF
        DECLARE @i INT
        SET @i = 0
        INSERT  INTO #FIDS
                ( id, levelNo )
        VALUES  ( @FolderId, -- id - int
                  1  -- levelNo - int
                  )
        WHILE @@ROWCOUNT > 0 
            BEGIN
                SET @i = @i + 1
                INSERT  #FIDS
                        SELECT  F.ID ,
                                @i + 1
                        FROM    #FIDS R ,
                                dbo.Doc_Folder F
                        WHERE   F.ParentDirID = R.id
                                AND R.levelNo = @i
            END
            
        DELETE  FROM dbo.Doc_Folder
        WHERE   ID IN ( SELECT  ID
                        FROM    #FIDS )
    END



你可能感兴趣的:(SQL删除树形结构)