简体   繁体   中英

Updating a column in every table in a schema in SQL Server

I want to update the column Last_Modified in every table in a given schema. This column is updated with latest timestamp if another column in the same table ( ENDTIME ) is updated.

To do this I have the following script in SQL Server:

DECLARE @TotalRows FLOAT
SET @TotalRows = (SELECT COUNT(*) FROM table1)

DECLARE @TotalLoopCount INT
SET @TotalLoopCount = CEILING(@TotalRows / 100000)

DECLARE @InitialLoopCount INT
SET @InitialLoopCount = 1

DECLARE @AffectedRows INT
SET @AffectedRows = 0

DECLARE @intialrows INT;
SET @intialrows = 1

DECLARE @lastrows INT
SET @lastrows = 100000;

WHILE @InitialLoopCount <= @TotalLoopCount
BEGIN
    WITH updateRows AS
    (
        SELECT 
            t1.*, 
            ROW_NUMBER() OVER (ORDER BY caster) AS seqnum 
        FROM
            table1 t1
    )
    UPDATE updateRows 
    SET last_modified = ENDTIME AT TIME ZONE 'Central Standard Time'
    WHERE last_modified IS NULL 
      AND updateRows.ENDTIME IS NOT NULL
      AND updateRows.seqnum BETWEEN @intialrows AND @lastrows; 

    SET @AffectedRows = @AffectedRows + @@ROWCOUNT
    SET @intialrows = @intialrows + 100000
    SET @lastrows = @lastrows + 100000

    -- COMMIT
    SET @Remaining = @TotalRows - @AffectedRows
    SET @InitialLoopCount = @InitialLoopCount + 1
END

This script determines the count of a table, divides it by 100000 and runs only that many loops to perform the entire update. It breaks down the update in batches/loops and then perform updates on certain rows until it completes updating them all.

This script is only for 1 table, ie table1. I want to now modify this script in such a way that it dynamically takes all the tables in a schema and runs the above script for each of them. Let's say the schema name is schema1 and it has 32 tables, so this script should run for all those 32 tables.

I am able to retrieve the tables in schema1 but I am not able to dynamically send those to this script. Can anyone please help me with this?

To dynamically change table names at runtime you're going to need something like sp_executesql. See here for an example of its use: https://stackoverflow.com/a/3556554/22194

Then you could have an outer cursor that fetches the table names and then assembles the queries in a string and executes them. Its going to look horrible though.

If your schema doesn't change much another approach would be to generate a long script with a section for each table. You generate the script by querying the table names and then repeating the script with each different table name. Excel is actually pretty good for doing that sort of thing - paste your table names into Excel, use Excel to generate the script then copy/paste it back into SSMS.

This will be a long repetitive script but will avoid the disadvantage of having all the SQL in strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM