【问题标题】:Update many columns automatically自动更新许多列
【发布时间】:2022-01-22 15:38:04
【问题描述】:

如果有超过 100 个列,我想更新许多列,而不必多次手动键入 set = , set = , set = 语法。

如何自动更新所有列?

update table1
set t1.col1 = t2.col1, t1.col2 = t2.col2 ...........
from table1 as t1 inner join table2 as t2

我尝试使用info.schema 将所有列名称作为字符串保存在@variable 中。 但找不到将@variable 用作更新语句的一部分的方法。

例如:

@variable is  nvarchar (max)

@variable =       t1.col1 = t2.col1, t1.col2 = t2.col2, t1.col3 = t2.col3 

等等……

试图执行一个 SQL 变量 执行:

@sql_query = 'update set' + @variable +
from table1 as t1 inner join table 2 as t2

【问题讨论】:

  • 你可以考虑动态SQL...
  • SQL-Server Management Studio 可以为每个表生成一个 UPDATE 查询。
  • Excel 还有助于构建大列数 SQL 语句,尤其是当来自不同表的列具有相同名称时。
  • 乍一看,最好的解决方案是聘请 DBA,因为您似乎在寻找惰性解决方案而不是 DBA 解决方案!除非您对此要求有technical 的理由,否则要求在查询中使用较短的文本不是专业要求,而是懒惰要求!您可以一次动态构建查询字符串(使用查询或使用 excel),但将文本用作直接查询而不是动态查询,除非有原因(例如您需要动态更新的列) - @ 987654329@

标签: sql sql-server tsql sql-update


【解决方案1】:

如果你愿意,可以探索一下。 确保阅读其中的 cmets,并注意所有更新。 始终打印动态 sql 并在执行前检查它。

-- Make sure to use proper DataBase name for INFORMATION_SCHEMA.
-- When you update try to
-- always use database name and schema name in FROM as db.dbo.table
---------------------------------------------------------------------

use TestDB;
go


if OBJECT_ID('tempdb..#Columns') is null
    begin
        create table #Columns(
            columnName varchar(100)
        );
    end
else
    begin
        truncate table #Columns;
    end;


with cte_source as(
    select
        ('src.' + COLUMN_NAME) srcColumn
        , COLUMN_NAME columnName

    from INFORMATION_SCHEMA.COLUMNS

    where TABLE_NAME = 'sourceTable' -- your source table name
)
,cte_target as(
    select
        ('trg.' + COLUMN_NAME) trgColumn
        , COLUMN_NAME columnName

    from INFORMATION_SCHEMA.COLUMNS

    where TABLE_NAME = 'targetTable' -- your target table name
)
-- Pay attention, it joins only the same column name, if it is a table duplicate only.

insert into #Columns(
    columnName
)
select
    trg.trgColumn + ' = ' + src.srcColumn

from cte_source src
    inner join
    cte_target trg
    on trg.columnName = src.columnName


declare @mainCursor cursor;
declare @columnName varchar(100);
declare @setString varchar(1000);
declare @sql varchar(8000);


set @setString = '';
set @mainCursor = cursor for(
    select
        columnName
    from #Columns
)

open @mainCursor
fetch next from @mainCursor into
    @columnName

while @@fetch_status = 0
    begin
        set @setString += @columnName + ','

        fetch next from @mainCursor into
            @columnName
    end;

    set @setString = left(@setString, (len(@setString) - 1));

close @mainCursor;
deallocate @mainCursor;

print(@setString);

-- add your sql in to string, make sure to write correct aliases to tables as in update set string.
-------------------------------------------------------------------------------------------------------

set @sql = '
    update {your string}
        set ' + @setString + '

    from {your string}
'

print(@sql)

-- In order to execute update just do:     execute(@sql)
----------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2015-05-13
    • 2015-09-09
    • 1970-01-01
    • 2013-08-16
    相关资源
    最近更新 更多