问题
So I have an intermitant problem that I need a temporary fix for.
(something is causing issues with two tables intermittently (every 3-4 weeks))
One table gets completly cleared out and another seems to have individual records removed...
as this is an ongoing issue we are still investigating the main cause but need a temporary fix.
As such I have setup a second "Backup" table for each of these tables and am trying to setup triggers to copy ALL Insert and Update commands
I have created the Insert triggers with no problem.
ALTER Trigger [dbo].[MainInsertBackup]
On [dbo].[tblMain] AFTER INSERT AS
BEGIN
INSERT INTO tblMainBackup
SELECT * FROM inserted
END
now I need a trigger that does the same for updates (performs the updates on the tblMainBackup).
this way I should ensure that the backup table has all of the information from the main but if a record is removed somehow it can be copied back to the main table from the backup table.
Only examples I have found perform updates in individual fields on the update, or insert a whole new record on the update into a log table.
any help is apriciated.
回答1:
You can try to use this UPDATE trigger to update the rows in tblMainbackup while update row in tblMain table
CREATE Trigger [dbo].[MainUpdateBackup]
On [dbo].[tblMain] AFTER UPDATE AS
BEGIN
UPDATE tblMainbackup
SET tblMainbackup.col1 = i1.col1,
tblMainbackup.col2 = i1.col2,
tblMainbackup.col3 = i1.col3
FROM tblMainbackup t1
INNER JOIN inserted i1
ON t1.Id = i1.ID
END
来源:https://stackoverflow.com/questions/27903254/sql-triggers-to-duplicate-update-changes-to-another-table