Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:13:24+00:00 2026-06-06T10:13:24+00:00

I want to create stored procedure that call recursion function, each time it will

  • 0

I want to create stored procedure that call recursion function, each time it will execute update statement.

I have a table called document

documentId   folderId
----------   --------
1222         1
1256         2
1257         3

And a folder table:

folderId    parentFolder
--------    -----------
1             5
2             1
3             2 
5             null

My stored procedure will delete folder number (1), when it has been deleted, it should move all document in folder 1 and sub folder of 1 to the parent folder of 1.

How to do that?

CREATE FUNCTION fn_deleteSubFolderDocument
(
@folderId INT ,
@newFolderId INT
)
RETURNS NVARCHAR(100) 
AS
BEGIN
DECLARE @count2 int 
SET @count2=(SELECT COUNT(*) FROM dbo.tbl_document_folder WHERE parent_folder=@folderId)
UPDATE tbl_document SET folder_id=@newFolderId WHERE folder_id=@folderId

IF(@count2 !=0)
BEGIN
DECLARE @table TABLE(id INT IDENTITY(1,1),folderId INT,parentFolder int  )
INSERT INTO @table(folderId,parentFolder)
SELECT folder_id,parent_folder FROM dbo.tbl_document_folder WHERE parent_folder=folder_id
DECLARE @index INT =0
WHILE @index<@count2
BEGIN
SET @index=@index+1
RETURN fn_deleteSubFolderDocument((SELECT folderId FROM @table WHERE id=@index),(SELECT parentFolder FROM @table WHERE id=@index ))
END

END 

END
GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:13:27+00:00Added an answer on June 6, 2026 at 10:13 am

    I’m trying to replicate in SQLfiddle to try to walk down through the tree of dependencies you’ve illustrated as I think your problem is similar to this SO Answer

    ok – this will walk down your tree for you:

    create table document
    (
      [documentId] int,
      [folderId] int
    )
    insert into document
    values
    (1222, 1),
    (1256, 2),
    (1257, 3)
    
    
    create table folder
    (
      [folderId] int,
      [parentFolder] int null
    )
    insert into folder
    values
    (1, 5),
    (2, 1),
    (3, 2),
    (5, null)
    

    Here is the recirsive CTE that walks down the tree and finds the subfolders of subfolders of subfolders …

    DECLARE @folderToDelete int = 1
    
    ;WITH RESULT (folders,LEVEL)
      AS
      (
      --anchor
       SELECT
          folderId [folders]
          ,0 AS LEVEL
       FROM folder AS E 
       WHERE folderId = @folderToDelete
    
      UNION ALL
      SELECT 
          E.folderId [folders]
          ,LEVEL +1 --switched parent/child
      FROM 
          folder AS E
          INNER JOIN RESULT AS D  
             ON 
              E.parentFolder=D.folders 
      --WHERE LEVEL < 100
      )
    SELECT *
    FROM RESULT OPTION (MAXRECURSION 100)
    

    I’ve saved a live example here on SQLfiddle

    Once you have this set of folders it should be easy enough to find all the associated documents that you need to move

    Referring to this article by Pinal Dave I’ve amended the way the loop detects it’s maximum. Without any sort of catch for the maximum if the loop carried on past 100 iterations the server would error but according to this article it looks like MAXRECURSION can be set as high as 32767….hopefully your file structure isn’t that complex!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a stored procedure that will create 3 triggers (insert, update, delete) given
I have stored procedure that I created in MySQL and want PHP to call
I want to create a stored procedure (on SQL Server 2005) that fetches a
I have a stored procedure in which i want to create a user defined
I have information stored in a database that I want to use to create
I have a really simple stored procedure that looks like this: CREATE PROCEDURE _Visitor_GetVisitorIDByVisitorGUID
I am using SQL Server 2005. I want to create a stored procedure that
I want to create a stored procedure with 4 insert and i have this
I have a stored procedure that returns all fields of an object. CREATE PROCEDURE
I want to write a Stored procedure...which will create a table with xyz name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.