简体   繁体   English

我正在寻找一种仅在表1的值发生变化的情况下将触发器插入第二个表的方法

[英]I am looking for a way for a trigger to insert into a second table only where the value in table 1 changes

I am looking for a way for a trigger to insert into a second table only where the value in table 1 changes. 我正在寻找一种仅将表1中的值更改的地方插入第二张表的触发器的方法。 It is essentially an audit tool to trap any changes made. 从本质上讲,它是一种捕获任何更改的审核工具。 The field in table 1 is price and we want to write additional fields. 表1中的字段是price,我们要编写其他字段。

This is what I have so far. 到目前为止,这就是我所拥有的。

CREATE  TRIGGER zmerps_Item_costprice__update_history_tr ON [ITEM] 
FOR UPDATE
AS
insert into zmerps_Item_costprice_history
        select  NEWID(), -- unique id
                GETDATE(),  -- CURRENT_date
                'PRICE_CHANGE', -- reason code
                a.ima_itemid, -- item id
                a.ima_price-- item price

            FROM  Inserted b inner join item a
                    on b.ima_recordid = a.IMA_RecordID

The table only contains a unique identifier, date, reference(item) and the field changed (price). 该表仅包含唯一标识符,日期,参考(项目)和更改的字段(价格)。 It writes any change not just a price change 它会写任何变化,而不仅仅是价格变化

Is it as simple as this? 这样简单吗? I moved some of the code around because comments after the comma between columns is just painful to maintain. 我移动了一些代码,因为列之间的逗号后的注释很难维护。 You also should ALWAYS specify the columns in an insert statement. 您还应该始终在插入语句中指定列。 If your table changes this code will still work. 如果您的表更改,此代码仍将起作用。

CREATE  TRIGGER zmerps_Item_costprice__update_history_tr ON [ITEM] 
FOR UPDATE
AS
insert into zmerps_Item_costprice_history
(
    UniqueID
    , CURRENT_date
    , ReasonCode
    , ItemID
    , ItemPrice
)
    select  NEWID()
        , GETDATE()
        , 'PRICE_CHANGE'
        , d.ima_itemid
        , d.ima_price
    FROM Inserted i 
    inner join deleted d on d.ima_recordid = i.IMA_RecordID
        AND d.ima_price <> i.ima_price

Since you haven't provided any other column names I Have used Column2 and Column3 and the "Other" column names in the below example. 由于您没有提供任何其他列名,因此在下面的示例中,我使用了Column2和Column3以及“其他”列名。

You can expand adding more columns in the below code. 您可以在下面的代码中展开添加更多列。

overview about the query below: 有关以下查询的概述:

Joined the deleted and inserted table (only targeting the rows that has changed) joining with the table itself will result in unnessacary processing of the rows which hasnt changed at all. 将已删除和插入的表(仅针对已更改的行)连接到表本身,将导致对根本没有更改的行进行不必要的处理。

I have used NULLIF function to yeild a null value if the value of the column hasnt changed. 如果列的值没有更改,我已经使用NULLIF函数来产生一个空值。

converted all the columns to same data type (required for unpivot) . 将所有列转换为相同的数据类型(unpivot必需)。

used unpivot to eliminate all the nulls from the result set. 使用unpivot从结果集中消除所有空值。

unpivot will also give you the column name its has unpivoted it. unpivot还将为您提供已取消透视的列名。

CREATE  TRIGGER zmerps_Item_costprice__update_history_tr 
ON [ITEM] 
FOR UPDATE
AS 
BEGIN
  SET NOCOUNT ON ;

WITH CTE AS (
SELECT  CAST(NULLIF(i.Price   , d.Price)    AS NVARCHAR(100)) AS Price
       ,CAST(NULLIF(i.Column2 , d.Column2)  AS NVARCHAR(100)) AS Column2
       ,CAST(NULLIF(i.Column3 , d.Column3)  AS NVARCHAR(100)) AS Column3
FROM dbo.inserted i 
INNER JOIN dbo.deleted d ON i.IMA_RecordID = d.IMA_RecordID
WHERE i.Price   <> d.Price
 OR   i.Column2 <> d.Column2
 OR   i.Column3 <> d.Column3
 )
 INSERT INTO zmerps_Item_costprice_history
               (unique_id, [CURRENT_date], [reason code], Item_Value)
 SELECT  NEWID()
        ,GETDATE()
        ,Value
        ,ColumnName + '_Change'
 FROM CTE UNPIVOT (Value FOR ColumnName IN (Price , Column2, Column3) )up

END

As I understand your question correctly, You want to record change If and only if The column Price value is changes, you dont need any other column changes to be recorded 据我正确理解您的问题,您要记录更改仅且仅当列价格值已更改时,您才需要记录其他任何列更改

here is your code 这是你的代码

CREATE  TRIGGER zmerps_Item_costprice__update_history_tr ON [ITEM] 
FOR UPDATE
AS
if update(ima_price)
insert into zmerps_Item_costprice_history
    select  NEWID(), -- unique id
            GETDATE(),  -- CURRENT_date
            'PRICE_CHANGE', -- reason code
            a.ima_itemid, -- item id
            a.ima_price-- item price

        FROM  Inserted b inner join item a
                on b.ima_recordid = a.IMA_RecordID

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

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