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 8973545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:28:17+00:00 2026-06-15T18:28:17+00:00

I have a file system app that uses a SQL Server 2008 R2 to

  • 0

I have a file system app that uses a SQL Server 2008 R2 to keep track of files. When I remove a file, I want to update parents in the path hierarchy to reflect their new size. I am using @fid for the current FILE_ID, @size as the FILE_SIZE, and @pid as the FILE_ID of the parent in the hierarchy.

Here is the loop I am using:

SELECT @pid=PARENT_ID FROM FILES WHERE FILE_ID=@fid;

WHILE @pid<>0
BEGIN

    UPDATE FILES
    SET 
        FILE_SIZE = 
        -- Avoid potential situation where new file size might incorrectly drop below 0
        CASE 
            WHEN FILE_SIZE-@size>=0 THEN FILE_SIZE-@size
            ELSE 0
        END
    WHERE FILE_ID=@pid;

    SET @fid=@pid;
    SELECT @pid=PARENT_ID FROM FILES WHERE FILE_ID=@fid;
END

When I run this, the sizes are not updating. If I replace the UPDATE with a SELECT, it looks like it should be working correctly. What is going on? Why are the sizes not getting updated? Is there a better way to do this?

To add some context, this snip-it is actually running inside another loop so multiple files can be deleted in a batch. Here is the code in this context:

-- Declarations
DECLARE @fid int, @size int, @pid int;
DECLARE c CURSOR FOR 
SELECT 
    FILE_ID, FILE_SIZE 
FROM
    FILES

OPEN c;

-- Initialize variables
FETCH NEXT FROM c 
INTO @fid, @size;

-- Main loop
WHILE @@FETCH_STATUS = 0
BEGIN

    -- Statements to delete the file --

    -- Loop to update sizes --
    SELECT @pid=PARENT_ID FROM FILES WHERE FILE_ID=@fid;
    WHILE @pid<>0
    BEGIN

        UPDATE FILES
        SET 
            FILE_SIZE = 
            CASE 
                WHEN FILE_SIZE-@size>=0 THEN FILE_SIZE-@size
                ELSE 0
            END
        WHERE FILE_ID=@pid;

        SET @fid=@pid;
        SELECT @pid=PARENT_ID FROM FILES WHERE FILE_ID=@fid;
    END

   FETCH NEXT FROM c 
   INTO @fid, @size;
END
CLOSE c;
DEALLOCATE c;
  • 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-15T18:28:18+00:00Added an answer on June 15, 2026 at 6:28 pm

    There is definately a better way to do this. The approach you outline is procedural and iterative and great for languages like C# and visual basic, however, for the most efficeint solution in SQL Cursors are the LAST trick you should reach for.

    This should work

    WITH    FileList
    AS
    (
        -- Select all the files that have no children
        SELECT   f.FILE_ID 
                ,f.FILE_SIZE
                ,f.PARENT_ID
                ,0 AS CHILD_ID
                ,0 AS Depth
        FROM    FILES f
                LEFT JOIN
                FILES f1 ON f.FILE_ID=f1.PARENT_ID
        WHERE   f1.PARENT_ID IS NULL
        UNION ALL
        -- Then recursively select thrir parents
        SELECT   f.FILE_ID
                ,fl.FILE_SIZE
                ,f.PARENT_ID
                ,fl.FILE_ID
                ,fl.Depth + 1
        FROM    FileList fl
                INNER JOIN
                FILES f ON f.FILE_ID=fl.PARENT_ID
    )
    -- With this data update the file size with the sum of all leaf nodes
    UPDATE  FILES
    SET     FILE_SIZE = (SELECT SUM(FILE_SIZE)
                        FROM    FileList fl
                        WHERE   fl.FILE_ID=FILES.FILE_ID
                        GROUP BY    FILE_ID)
    

    One command, no itteration – should be orders of magnitude quicker and you are getting the accurate filesizes propagated all the way up from the leaves each time.

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

Sidebar

Related Questions

I have a C# app that runs a jar file and uses the System.exit
Using Visual Studio 2008 and VB.Net: I have a working web app that uses
I have a PHP app that uses a $_GET parameter to select JS/CSS files
I have a System app that uses system permissions and I have those permissions
I have defined two endpoints in my App.Config file as <system.serviceModel> <services> <service name=HostDirectAddress.ITestService
I have a simple file-system-ed-login (not mysql) just to track number of users who
I have been looking into libraries for a file system that will allow path
I've got an app that uses Sql CE 3.5 SP2. I've included the DLL's
I have an XML file that specifies the image files that I need to
I have a winforms app that uses a strongly typed custom DataSet for holding

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.