我想在数据库中创建一个触发器,该触发器应在表'Valve'更新时运行。
作为触发条件的一部分,必须发生以下情况:
这些更改应根据源Valve在表ME中自动发生,而无需任何手动干预。
请帮助我创建这样的触发器?我是SQL新手。
在我的表ME.Valve上,我想添加其他列,这些列具有新的信息,这些信息我不想出现在Valve表中(不要破坏它)。我在现有的表Valve上附上图片。 因此,我想要从表SQL_P3D_Test_ME中的表“ Valve_PNP”复制,例如仅复制“ LineNumberTag”,“ Tag”列,但是当表“ Valve_PNP”列的“ LineNumberTag”,“ Tag”值更改时,要进行更改并自动进入我的表格SQL_P3D_Test_ME。 然后在我的表格SQL_P3D_Test_ME中添加新列,例如在每个阀门前面的行中手动插入信息。 有可能吗?
这就是我想要的,在表“ dbo.Source Tabel”中,您只有一行,其中包含“ TextColumn”,“ ValueColumn”等列中的信息。首先,此信息将复制到表[me]中。 [目标表],但是当信息输入到表“ dbo.Source Tabel”中时,例如“ TextColumn”列中的信息从“测试插入和数据不变地更新”中用“ Marius”更改为表[me]中的更新。 [表格]仅此信息,而不添加新行。对于每一列,依此类推。
我使用Microsoft SQL Server Management Studio
答案 0 :(得分:0)
我建议将触发器添加到“ Valve”表中,该表包含对“ ME.Valve”表的更新。让我知道是否应该编写语法示例。
这是我的解决方案的一个例子。
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