简体   繁体   English

比较来自两个不同表的两行以获取更新的列

[英]Comparing two rows from two different tables for updated columns

I have two tables, one history and one actual 我有两张桌子,一张历史记录,一张实际

HistoryTable 历史表

ActualCalcID DSID        FormatID    RollupId    BD                                       BID         DFID        FMID        Year        Name                                     TID         SID         TyId
------------ ----------- ----------- ----------- ---------------------------------------- ----------- ----------- ----------- ----------- ---------------------------------------- ----------- ----------- -----------
18           40          3           1           Test Edit VB                             2           1           1           2016        CCG Contribution Margin (000s) - Test    4           101         3
18           40          3           1           Test Edit VB1                            2           1           1           2016        CCG Contribution Margin (000s) - Test1   4           101         3

ActualTable 实际表

ActualCalcID DSID        FormatID    RollupId    BD                                       BID         DFID        FMID        Year        Name                                     TID         SID         TyId
------------ ----------- ----------- ----------- ---------------------------------------- ----------- ----------- ----------- ----------- ---------------------------------------- ----------- ----------- -----------
18           40          4           2           Test Edit VB2                            2           1           2           2016        CCG Contribution Margin (000s) - Test2   5           101         5

When user clicks on a button in UI, the row from ActualTable is moved to HistoryTable with the help of an trigger (basically Archiving). 当用户单击UI中的按钮时,在触发器的帮助下(基本上是存档),ActualTable中的行将移至HistoryTable。

I need which columns have changed when the trigger run completed, in below format 我需要在触发器运行完成后更改以下列,格式如下

Changed Field | Changed Field Content

FormatId | 3

Rollup Id | 2

BD | Test Edit VB1

FMID | 2

Name | CCG Contribution Margin (000s) - Test1

TID | 5

TypeId | 5

I think I did not ask my question properly. 我想我没有适当地提出问题。 I want to compare the latest row from history table and the row from actual tables to check which columns have changed. 我想比较历史记录表中的最新行和实际表中的行,以检查哪些列已更改。 It might be different at different times, some times there will be 4 column changed, sometimes 5, sometimes 1. the output table needs to be logged with only entry for changed column. 在不同的时间可能会有所不同,有时会更改4列,有时会更改5,有时1.记录输出表时只需要记录更改列的条目。

You can use UNION ALL : 您可以使用UNION ALL

SELECT 'FormatId' AS [Changed Field], CONVERT(VARCHAR(100), FormatId) AS [Changed Field Content] FROM Actual UNION ALL
SELECT 'Rollup Id', CONVERT(VARCHAR(100), RollupId) FROM Actual UNION ALL
SELECT 'BD', CONVERT(VARCHAR(100), BD) FROM Actual UNION ALL
SELECT 'FMID', CONVERT(VARCHAR(100), FMID) FROM Actual UNION ALL
SELECT 'Name', CONVERT(VARCHAR(100), Name) FROM Actual UNION ALL
SELECT 'TID', CONVERT(VARCHAR(100), TID) FROM Actual UNION ALL
SELECT 'TypeId', CONVERT(VARCHAR(100), TID) FROM Actual

Use Table valued constructor with cross apply to unpivot the data 使用Table valued constructorcross apply取消数据透视

select [Changed Field] , [Changed Field Content]
from yourtable
cross apply
(
values ('FormatId',CONVERT(VARCHAR(100), FormatId)),
(Rollup Id', CONVERT(VARCHAR(100), RollupId)),
('BD', CONVERT(VARCHAR(100), BD)),
('FMID', CONVERT(VARCHAR(100), FMID)),
('Name', CONVERT(VARCHAR(100), Name)),
('TID', CONVERT(VARCHAR(100), TID)),
('TypeId', CONVERT(VARCHAR(100), TID))
) 
cs ([Changed Field] , [Changed Field Content])

You can use OUTPUT after your INSERT to get the inserted row. 您可以在INSERT之后使用OUTPUT来获取插入的行。 You need to apply this in your trigger. 您需要在触发器中应用它。

DECLARE @TableVar TABLE(
    ChangedField VARCHAR(50)                       
    ,FormatId INT
    ,RollupId INT
    ,BD VARCHAR(50)
    ,FMID INT
    ,Name VARCHAR(250)
    ,TID INT
    ,TypeId INT
)

INSERT HistoryTable
    OUTPUT 'Changed Field Content'
            ,inserted.FormatId
            ,inserted.RollupId
            ,inserted.BD
            ,inserted.FMID
            ,inserted.Name
            ,inserted.TID
            ,inserted.TypeId
        INTO @TableVar
VALUES ('', '', ...)

SELECT * FROM @TableVar

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

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