如何创建 SQL 触发器以在更新另一个 table 时更新一个 table 上的日期字段

How to create a SQL trigger to update a date field on one table when another table is updated

我想在我的数据库中创建一个触发器,当 table 'Valve' 更新时应该 运行。

作为触发器的一部分,必须发生以下情况:

  1. ME table 应根据 Valve table [PnPID,位置 X/Y/Z,LineNumberTag,标记,规格]
  2. 的架构创建或更新
  3. 现有tableValve的所有信息,待copied/updated进入tableME

更改应该会根据源 Valve 在 table ME 中自动发生,无需任何手动干预。

请帮我创建这样一个触发器?我是 SQL.

的新人

在我的 table ME.Valve 上,我想添加其他列,其中包含我不想出现在 Valve table 中的新信息(不要破坏它)。我附上一张现有 table Valve 的图片。 所以,我想要的是从 tabel "Valve_PNP" 复制到我的 tabel SQL_P3D_Test_ME 例如只有列 "LineNumberTag" , "Tag",但是当来自 tabel "Valve_PNP" 列的值"LineNumberTag" , "Tag" 正在变化,自动变化并进入我的表格 SQL_P3D_Test_ME 。 在我的表格 SQL_P3D_Test_ME 中为 中制作的示例添加新列 我在每个阀门前面的行中手动插入信息。 有可能吗?

这就是我想要的,在 table "dbo.Source Tabel" 中有一行,其中有 "TextColumn"、"ValueColumn" 等列中的信息。对于开始此信息将在 table [me].[Destination Table] 中复制,但是当信息进入 tabel"dbo.Source Tabel" 时,例如在列 "TextColumn" 中从 "test insert and update with no data change" 更改为 "Marius",以便在表格 [me].[Destination Table] 中仅更新此信息而不添加新行。每列依此类推。

我使用 Microsoft SQL Server Management Studio

我建议将触发器添加到包含 "ME.Valve" table 更新的 "Valve" table。如果我应该写一个语法示例,请告诉我。

这是我的解决方案示例..

USE [SpecifyYourDatabaseHere]

CREATE TABLE dbo.SourceTable
    (
    SourcePrimaryKeyID INT IDENTITY PRIMARY KEY,
    TextColumn VARCHAR(2048),
    ValueColumn DECIMAL(18,3),
    NumberColumn INT
    )

CREATE TABLE [me].[DestinationTable]
    (
    DestinationPrimaryKeyID INT IDENTITY PRIMARY KEY,
    SourcePrimaryKeyID INT,
    TextColumn VARCHAR(2048),
    ValueColumn DECIMAL(18,3),
    NumberColumn INT,
    ActionType VARCHAR(100),
    CreatedDate AS GETDATE(), -- Default to current date
    CreatedDatabaseUser AS SUSER_SNAME()
    )

GO

-- You will need an insert, update and delete trigger.

-- INSERT TRIGGER

-- This trigger will insert any new records into the destination table.
CREATE TRIGGER [dbo].[SourceTable_Insert]
ON [dbo].[SourceTable]
FOR INSERT

AS 

INSERT INTO [me].[DestinationTable] 
    (
    SourcePrimaryKeyID,
    TextColumn,
    ValueColumn,
    NumberColumn,
    ActionType
    )
SELECT  INSERTED.SourcePrimaryKeyID,
        INSERTED.TextColumn,
        INSERTED.ValueColumn,
        INSERTED.NumberColumn,
        'Insert' AS ActionType
FROM    INSERTED

GO

-- UPDATE TRIGGER

-- Conditional Update Trigger : This trigger will only insert data in destination of any of the values in the data columns has changed (Saves space).
CREATE TRIGGER [dbo].[SourceTable_Update]
ON [dbo].[SourceTable]
FOR UPDATE

AS 

INSERT INTO [me].[DestinationTable] 
    (
    SourcePrimaryKeyID,
    TextColumn,
    ValueColumn,
    NumberColumn,
    ActionType
    )
SELECT  INSERTED.SourcePrimaryKeyID,
        INSERTED.TextColumn,
        INSERTED.ValueColumn,
        INSERTED.NumberColumn,
        'Update' AS ActionType
FROM    INSERTED
        INNER JOIN
            (
            SELECT  SourcePrimaryKeyID,TextColumn,ValueColumn,NumberColumn
            FROM    [me].[DestinationTable]
                    INNER JOIN
                        (
                        SELECT  MAX(DestinationPrimaryKeyID) MaxDestinationPrimaryKeyID,
                                COUNT(1) DestinationRecordCount
                        FROM    [me].[DestinationTable]
                                INNER JOIN
                                    INSERTED ON
                                        [me].[DestinationTable].SourcePrimaryKeyID = INSERTED.SourcePrimaryKeyID
                        GROUP BY
                            [me].[DestinationTable].SourcePrimaryKeyID
                        ) MaxDestinationPrimaryKey ON
                        MaxDestinationPrimaryKey.MaxDestinationPrimaryKeyID = [me].[DestinationTable].DestinationPrimaryKeyID
            ) DestinationData ON
                DestinationData.SourcePrimaryKeyID = INSERTED.SourcePrimaryKeyID
                AND (
                        ISNULL(DestinationData.TextColumn,'') != ISNULL(INSERTED.TextColumn,'') OR 
                        ISNULL(DestinationData.ValueColumn,0) != ISNULL(INSERTED.ValueColumn,0) OR 
                        ISNULL(DestinationData.NumberColumn,0) != ISNULL(INSERTED.NumberColumn,0)
                    )

GO

-- DELETE TRIGGER

-- This trigger will insert any deleted records into the destination table.

CREATE TRIGGER [dbo].[SourceTable_Delete]
ON [dbo].[SourceTable]
FOR DELETE

AS 

INSERT INTO [me].[DestinationTable] 
    (
    SourcePrimaryKeyID,
    TextColumn,
    ValueColumn,
    NumberColumn,
    ActionType
    )
SELECT  INSERTED.SourcePrimaryKeyID,
        INSERTED.TextColumn,
        INSERTED.ValueColumn,
        INSERTED.NumberColumn,
        'Delete' AS ActionType
FROM    INSERTED

GO

-- Test the code..

-- Insert trigger 
INSERT  [dbo].[SourceTable]
SELECT  'test insert and update with no data change',
        123.456,
        1

INSERT  [dbo].[SourceTable]
SELECT  'test insert and update with data changed',
        123.456,
        1

INSERT  [dbo].[SourceTable]
SELECT  'test delete',
        123.456,
        1

-- Update trigger test 1 - no data changed (Must not add record in destination)
UPDATE  [dbo].[SourceTable]
SET     NumberColumn = 1
WHERE   SourcePrimaryKeyID = 1

-- Update trigger test 2 - Data changed (Must add record in destination)
UPDATE  [dbo].[SourceTable]
SET     NumberColumn = NumberColumn + 1
WHERE   SourcePrimaryKeyID = 2

-- Deleted trigger test
DELETE FROM [dbo].[SourceTable]
WHERE   SourcePrimaryKeyID = 3

SELECT  *
FROM    dbo.SourceTable

SELECT  *
FROM    me.DestinationTable
ORDER BY
        SourcePrimaryKeyID,
        DestinationPrimaryKeyID