Я предлагаю добавить триггер в таблицу «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