简体   繁体   English

在 SQL 服务器中的模式中更新每个表中的列

[英]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.我想更新给定模式中每个表中的Last_Modified列。 This column is updated with latest timestamp if another column in the same table ( ENDTIME ) is updated.如果同一表中的另一列 ( ENDTIME ) 更新,则此列将更新为最新时间戳。

To do this I have the following script in SQL Server:为此,我在 SQL 服务器中有以下脚本:

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.此脚本确定表的计数,将其除以 100000,然后只运行那么多循环来执行整个更新。 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.此脚本仅适用于 1 个表,即 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.假设模式名称是 schema1,它有 32 个表,所以这个脚本应该为所有这 32 个表运行。

I am able to retrieve the tables in schema1 but I am not able to dynamically send those to this script.我能够检索 schema1 中的表,但无法将它们动态发送到此脚本。 Can anyone please help me with this?谁能帮我解决这个问题?

To dynamically change table names at runtime you're going to need something like sp_executesql.要在运行时动态更改表名,您将需要 sp_executesql 之类的东西。 See here for an example of its use: https://stackoverflow.com/a/3556554/22194有关其使用示例,请参见此处: 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.然后你可以有一个外部 cursor 获取表名,然后将查询组合成一个字符串并执行它们。 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. Excel 实际上非常适合做这类事情 - 将表名粘贴到 Excel,使用 Excel 生成脚本,然后将其复制/粘贴回 SSMS。

This will be a long repetitive script but will avoid the disadvantage of having all the SQL in strings.这将是一个冗长的重复脚本,但可以避免在字符串中包含所有 SQL 的缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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