创建一个触发器,在更新列时将值插入新表中

时间:2021-08-04 00:48:53

I've been looking at some previous answers on triggers on here but can't find what I need exactly but I'm sure my question has been asked/answered before.

我一直在寻找一些关于触发器的先前答案,但是找不到我需要的东西,但我确定我的问题之前已经被问过/回答了。

I'm trying to keep track of any changes to columnA and columnB in table1.

我正在尝试跟踪table1中columnA和columnB的任何更改。

If this value changes I want to keep track of the values by inserting the existing value and the new Value into a different table with a date.

如果此值发生更改,我希望通过将现有值和新值插入带有日期的其他表来跟踪值。

I've been looking at using something like this for the insert but not sure how to add get the existing and new values of the source table (table1):

我一直在寻找使用类似这样的插入但不确定如何添加获取源表(table1)的现有值和新值:

CREATE TRIGGER NewTrigger ON table1
FOR INSERT
AS

INSERT INTO table2
        (columnA , columnB, todaysDate)
    .
    .

go

I need to use (I think) the

我需要使用(我认为)

Before update ON table1 FOR EACH ROW
   .
   .
   .
BEGIN

and look through all the changes and insert these first then do the same after the Update?

并查看所有更改并首先插入这些更改,然后在更新后执行相同操作?

3 个解决方案

#1


7  

Something like this should do what you need. You would have the INSERT statements below insert values indicating the operation performed into MyLogTable.

这样的事情应该做你需要的。您将在下面的INSERT语句中插入指示在MyLogTable中执行的操作的值。

CREATE TRIGGER [dbo].[TRIG_MyTable]
ON [dbo].[MyTable]
AFTER INSERT, UPDATE

AS 

DECLARE @INS int, @DEL int

SELECT @INS = COUNT(*) FROM INSERTED
SELECT @DEL = COUNT(*) FROM DELETED

IF @INS > 0 AND @DEL > 0 
BEGIN

    -- a record got updated, so log accordingly.

    INSERT INTO MyLogTable
    SELECT 'New Values', getdate() FROM INSERTED

    INSERT INTO MyLogTable
    SELECT 'Old Values', getdate() FROM DELETED

END

ELSE 
BEGIN

    -- a new record was inserted.

    INSERT INTO MyLogTable
    SELECT 'Insert', getdate() FROM INSERTED

END

If you wanted to you could also add columns from INSERTED and DELETED to your log table as well if you wanted to capture the actual column values that got inserted or updated.

如果您愿意,还可以将INSERTED和DELETED中的列添加到日志表中,如果您想捕获插入或更新的实际列值。

#2


4  

This is for all changes and all columns, but you can modify how you like:

这适用于所有更改和所有列,但您可以修改您喜欢的方式:

USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[trMyTrigger]
ON [dbo].[MyTable]
AFTER INSERT, UPDATE, DELETE
NOT FOR REPLICATION
AS
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with caller queries select statements.
    -- if an update/insert/delete occurs on the main table, the number of records affected
    -- should only be based on that table and not what records the triggers may/may not
    -- select.
SET NOCOUNT ON;

    -- Determine if this is an insert,update, or delete action

    DECLARE @action AS CHAR(1)
    DECLARE @count AS INT
    SET @action = 'I' -- SET action to 'I'NSERT by default.
    SELECT @count = count(*) FROM DELETED
    IF @count > 0
        BEGIN
            SET @action= 'D' -- SET action to 'D'ELETED.
            SELECT @count = count(*) FROM INSERTED
            IF @count > 0
                SET @action = 'U' -- SET action to 'U'PDATED.
        END

    IF @action = 'D'
        -- THIS IS A DELETE RECORD ACTION
        BEGIN
            INSERT INTO myBackupTable
        SELECT *,GETDATE() AS changeDate, 'DELETE' AS task FROM DELETED
        END
    ELSE
        BEGIN
            IF @action = 'I'
                 -- this is an INSERT record action
                BEGIN
                    INSERT INTO myBackupTable
                    SELECT *,GETDATE() AS changeDate, 'INSERT' as task FROM INSERTED
                END
             ELSE
                -- this is an UPDATE record action
                BEGIN
                    INSERT INTO myBackupTable
                    SELECT *,GETDATE() AS changeDate, 'UPDATE' as task  FROM INSERTED
                END
        END

#3


1  

create trigger trigge on abs
instead of update as

在abs上创建触发器触发而不是更新为

declare @idd int , @pricee money
  select @idd= ProductID from inserted 
  select @pricee = ListPrice from inserted 
  insert into prod values ( @idd , @pricee)
  print ' cannot change'

#1


7  

Something like this should do what you need. You would have the INSERT statements below insert values indicating the operation performed into MyLogTable.

这样的事情应该做你需要的。您将在下面的INSERT语句中插入指示在MyLogTable中执行的操作的值。

CREATE TRIGGER [dbo].[TRIG_MyTable]
ON [dbo].[MyTable]
AFTER INSERT, UPDATE

AS 

DECLARE @INS int, @DEL int

SELECT @INS = COUNT(*) FROM INSERTED
SELECT @DEL = COUNT(*) FROM DELETED

IF @INS > 0 AND @DEL > 0 
BEGIN

    -- a record got updated, so log accordingly.

    INSERT INTO MyLogTable
    SELECT 'New Values', getdate() FROM INSERTED

    INSERT INTO MyLogTable
    SELECT 'Old Values', getdate() FROM DELETED

END

ELSE 
BEGIN

    -- a new record was inserted.

    INSERT INTO MyLogTable
    SELECT 'Insert', getdate() FROM INSERTED

END

If you wanted to you could also add columns from INSERTED and DELETED to your log table as well if you wanted to capture the actual column values that got inserted or updated.

如果您愿意,还可以将INSERTED和DELETED中的列添加到日志表中,如果您想捕获插入或更新的实际列值。

#2


4  

This is for all changes and all columns, but you can modify how you like:

这适用于所有更改和所有列,但您可以修改您喜欢的方式:

USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[trMyTrigger]
ON [dbo].[MyTable]
AFTER INSERT, UPDATE, DELETE
NOT FOR REPLICATION
AS
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with caller queries select statements.
    -- if an update/insert/delete occurs on the main table, the number of records affected
    -- should only be based on that table and not what records the triggers may/may not
    -- select.
SET NOCOUNT ON;

    -- Determine if this is an insert,update, or delete action

    DECLARE @action AS CHAR(1)
    DECLARE @count AS INT
    SET @action = 'I' -- SET action to 'I'NSERT by default.
    SELECT @count = count(*) FROM DELETED
    IF @count > 0
        BEGIN
            SET @action= 'D' -- SET action to 'D'ELETED.
            SELECT @count = count(*) FROM INSERTED
            IF @count > 0
                SET @action = 'U' -- SET action to 'U'PDATED.
        END

    IF @action = 'D'
        -- THIS IS A DELETE RECORD ACTION
        BEGIN
            INSERT INTO myBackupTable
        SELECT *,GETDATE() AS changeDate, 'DELETE' AS task FROM DELETED
        END
    ELSE
        BEGIN
            IF @action = 'I'
                 -- this is an INSERT record action
                BEGIN
                    INSERT INTO myBackupTable
                    SELECT *,GETDATE() AS changeDate, 'INSERT' as task FROM INSERTED
                END
             ELSE
                -- this is an UPDATE record action
                BEGIN
                    INSERT INTO myBackupTable
                    SELECT *,GETDATE() AS changeDate, 'UPDATE' as task  FROM INSERTED
                END
        END

#3


1  

create trigger trigge on abs
instead of update as

在abs上创建触发器触发而不是更新为

declare @idd int , @pricee money
  select @idd= ProductID from inserted 
  select @pricee = ListPrice from inserted 
  insert into prod values ( @idd , @pricee)
  print ' cannot change'