【问题标题】:How to update a column in a table using joins(with another table) with duplicate values in the linking fields如何使用连接(与另一个表)在链接字段中具有重复值来更新表中的列
【发布时间】:2019-05-02 20:47:20
【问题描述】:

我有两个表:A 和 B。A 有五列,即 Co_ID、MODE、Ship_date、SCAC、BU、Condition B有以下列:Co_ID、Mode、Condition

A中的样本数据如下:

Co_ID   MODE    Ship_date   SCAC    BU  Condition
XYZ DV  10/5/2018   CEDD    XYZ 
XYZ DV  10/6/2018   CEDD    XYZ 
XYZ DV  10/6/2018   NAFQ    XYZ 
XYZ DV  10/7/2018   ABCD    XYZ 
XYZ DV  10/7/2018   PQRS    XYZ 
XYZ TC  10/4/2018   BLKW    XYZ 
XYZ TC  10/7/2018   BLKW    XYZ 
XYZ TC  10/7/2018   ABCD    XYZ 
XYZ TC  10/8/2019   PQRS    XYZ 

B中的样本数据如下:

Co_ID   Mode    Condition
XYZ DV  A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ DV  A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac Not in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ TC  A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ TC  A.Co_id = 'XYZ' and TransactionData_TL.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac not in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

目标:使用表 B 中连接 Co_ID 和模式列的条件列更新表 A 中的条件列。

我面临的问题:表 A 中的条件列更新错误,这意味着,例如,表 A 中 Co_ID='XYZ' 和 mode='DV' 的所有行(第 1 到 5 行)使用 Co_ID='XYZ' 和 mode='DV'(row 1) 错误地更新到条件列的第一个值,而不是表 B 各自的条件值。我无法理解确切的问题出在哪里.

我使用基于游标的查询将表 A 中的条件列更新为表 B 中的条件列,链接 Co_ID 和模式列,并在 where 子句中提供条件列,如下所示:

Declare @sql_program nvarchar(max), @sql nvarchar(max)
Set @sql_program=''
Set @sql=''

Declare CONDITION_CURSOR Cursor for

Select 'Update A
Set A.Condition = B.Condition
From A inner join B
on A.Co_ID = B.Co_ID and A.mode = B.mode  ' + ' and ' + 
CASE WHEN (LTRIM(RTRIM(B.Condition)) is null) THEN '' ELSE (LTRIM(RTRIM(B.Condition))) END
From B


Open CONDITION_CURSOR

Fetch next from CONDITION_CURSOR into @sql_program
While(@@FETCH_STATUS!=-1)
    Begin
        If(@sql_program is not null)
            Begin
                If(@sql is null or @sql = '')
                    Begin
                        Set @sql = @sql_program
                    End
                Else
                    Begin
                        Set @sql = @sql + ' ; '+ @sql_program
                    End
            end

Fetch next from CONDITION_CURSOR into @sql_program
End
Close CONDITION_CURSOR
Deallocate CONDITION_CURSOR
Select @sql as Condition_query
EXEC sp_executesql @sql

预期的输出应该如下:

Co_ID   MODE    Ship_date   SCAC    BU  Condition
XYZ DV  10/5/2018   CEDD    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW')and A.BU in('XYZ')

XYZ DV  10/6/2018   CEDD    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ DV  10/6/2018   NAFQ    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ DV  10/7/2018   ABCD    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac Not in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ DV  10/7/2018   PQRS    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac Not in('CEDD','NAFQ','BLKW')  and A.BU in('XYZ')

XYZ TC  10/4/2018   BLKW    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ TC  10/7/2018   BLKW    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ TC  10/7/2018   ABCD    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac not in('CEDD','NAFQ','BLKW')  and A.BU in('XYZ')

XYZ TC  10/8/2019   PQRS    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac not in('CEDD','NAFQ','BLKW')  and A.BU in('XYZ')

我目前得到的实际输出如下:

Co_ID   MODE    Ship_date   SCAC    BU  Condition
XYZ DV  10/5/2018   CEDD    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ DV  10/6/2018   CEDD    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ DV  10/6/2018   NAFQ    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ DV  10/7/2018   ABCD    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ DV  10/7/2018   PQRS    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ TC  10/4/2018   BLKW    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ TC  10/7/2018   BLKW    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ TC  10/7/2018   ABCD    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

XYZ TC  10/8/2019   PQRS    XYZ A.Co_id = 'XYZ' and A.Ship_date>='2018-10-01' and A.Ship_date<='2018-12-31' and A.mode in('TC') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

【问题讨论】:

  • 我会尝试PRINT @sql 并在执行之前查看它的外观。更好的是,首先看看游标中该选择语句的输出是什么......它看起来像你首先需要的吗?
  • @sql 基本上保存了游标的select语句的输出。游标的select语句的输出是这样的(下面写了一组语句供参考):sql -- select statement output Update A Set A.Condition = B.Condition From A inner join B on A.Co_ID = B.Co_ID and A.mode = B.mode where A.Ship_date&gt;='2018-10-01' and A.Ship_date&lt;='2018-12-31' and A.mode in('DV') and A.Scac in('CEDD','NAFQ','BLKW') and A.BU in('XYZ')

标签: sql-server tsql


【解决方案1】:

更新集合 a.condition = b.condition FROM tableA a 内连接 tableB b on a.Co_Id = b。 Co_Id

【讨论】:

  • 虽然查询没有返回任何错误,但输出是错误的,不符合我的问题要求。
猜你喜欢
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
相关资源
最近更新 更多