sqlsql-serverazure-data-studio

Create a script that loops deleting a number of rows in a table


I've tried to create a script that loops deleting a random number of rows from a table per campaign. It works for the first campaign but doesn't for the others

Below is my script. I declare the attendees deleted variable then I use the rand() function to generate a random number which I then use in my delete statement via using a CTE and setting a row number. It works for the first campaign but then doesn't for the rest even though when I look at the print statement, it shows the random number generated.

DECLARE @CampaignID INT
SET @CampaignID = 1

WHILE @CampaignID <= 1000
    BEGIN
        DECLARE @AttendeesDeleted INT

        SELECT @AttendeesDeleted = ROUND(RAND() * 10000, 0)

        ;WITH attendees AS (
            SELECT  *,
                    ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY  CampaignAttendeeID) AS rn
            FROM Warehouse.CampaignAttendees
        )
        DELETE
        FROM attendees 
        WHERE rn > @AttendeesDeleted
        AND CampaignID = @CampaignID

        PRINT 'Attendees Deleted: ' + CAST(@AttendeesDeleted AS VARCHAR)
            
        SET @CampaignID = @campaignID + 1
    END

Do I need to maybe create the random select script as a function then run it as a function?

query output


Solution

  • The WHERE CampaignID = @CampaignID part belongs at the SELECT statement, not at the DELETE.

    This change causes the ROW_NUMBER() to be calculated for the rows belonging to a campaign, instead of for all rows in the table.