0

我有两张表,Table1 和 Table2,它们有 N 号。的列。我需要根据 Table2 更新 Table1。我想更新 Table1 中的所有列,这些列在 Table2 的单个列中列出。

例如

    Table1   A B C D E . . .
             1 2 3 4 5 . . .
             7 6 5 4 3 . . .

    Table2   X Y Col_Nam Col_Value
             1 2    C       8
             1 2    D       9
             7 6    E       10
             7 6    C       20
             . .    .       .
             . .    .       .

当满足以下条件 Table1.A = Table2.X 和 Table1.B = Table2.Y 时,更新 Table1 中所有列在 Table2 中的列

平台是 SQL Server。我正在寻找的是一个动态的解决方案,因为我不知道我要更新的列名。表1可以有N个。需要更新的列。我使用光标尝试了以下操作。

DECLARE @s Varchar(MAX), @key1 VARCHAR(MAX), @key2 VARCHAR(MAX), @Cname VARCHAR(MAX), @CValue VARCHAR(MAX)

DECLARE crs CURSOR FOR SELECT * FROM Table2
OPEN crs;
FETCH NEXT FROM crs inTO @key1,@key2,@Cname,@Cvalue;
WHILE @@FETCH_STATUS = 0
BEGIN
set @s =
                'Update T1 SET ' +  @FieldName + ' = ''' + @FieldValue +
        ''' from Table1 T1' +
        ' where T1.A = '''  + @key1 +
        ''' and T1.B = ''' + @key2 

exec(@s)


    FETCH NEXT FROM crs inTO @key1,@key2,@Cname,@Cvalue;

END

CLOSE crs
DEALLOCATE crs

不知何故,它不起作用,我想为所有与 where 条件不匹配的记录设置一个默认值。

任何其他解决方案或帮助将不胜感激。

4

2 回答 2

1

警告:在使用任何动态 SQL 之前,请阅读SQL 注入。在您的情况下,如果用户有权访问 table2,则可以通过将 sql 代码写入 col_name 来破解它。

SQL 提琴示例

declare @X int, @Y int, @stmt nvarchar(max), @params nvarchar(max)

select @params = '@X int, @Y int'

declare table_cursor cursor local fast_forward for
    select distinct X, Y from Table2

open table_cursor
while 1 = 1
begin
    fetch table_cursor into @X, @Y
    if @@fetch_status <> 0 break

    select @stmt = null
    select @stmt = 
        isnull(@stmt + ', ', '') + 
        Col_Name + ' = ' + cast(Col_Value as nvarchar(max))
    from Table2
    where X = @X and Y = @Y

    select @stmt = 'update Table1 set ' + @stmt + ' where A = @X and B = @Y'

    exec dbo.sp_executesql
        @stmt = @stmt,
        @params = @params,
        @X = @X,
        @Y = @Y
end
close table_cursor
deallocate table_cursor
于 2013-07-28T19:16:14.037 回答
0

执行此操作的标准 SQL 方法需要相关子查询:

update table1
    set C = coalesce((select max(col_value)
                      from Table2 t2
                      where table1.A = t2.X and table1.B = t2.Y and
                            t2.Col_Name = 'A'
                     ), C),
        D = coalesce((select max(col_value)
                      from Table2 t2
                      where table1.A = t2.X and table1.B = t2.Y and
                            t2.Col_Name = 'D'
                     ), D)

一些 SQL 引擎允许连接。以下是与 MySQL 一起使用的方法:

update table1 join
       (select X, Y, max(case when col_name = 'C' then col_value end) as C,
               max(case when col_name = 'D' then col_value end) as D
        from table2
        group by X, Y
       ) t2
       on t2.X = table1.A and t2.Y = table2.Y
    set C = coalesce(t2.C, C),
        D = coalesce(t2.D, D)

在这两种情况下coalesce(),当不匹配时,都旨在保留当前值。如果您想要NULL不匹配,则只需删除coalesce().

编辑

在 SQL Server 中,更新/连接的语法略有不同:

update table1 join
    set C = coalesce(t2.C, C),
        D = coalesce(t2.D, D)
    from table1 join
         (select X, Y, max(case when col_name = 'C' then col_value end) as C,
                 max(case when col_name = 'D' then col_value end) as D
          from table2
          group by X, Y
         ) t2
         on t2.X = table1.A and t2.Y = table2.Y;
于 2013-07-27T16:20:32.093 回答