Differences
This shows you the differences between two versions of the page.
— |
control_sql_-_log_records_saved_without_changing_seqid [2019/01/27 11:29] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Explanation of SQL ====== | ||
+ | |||
+ | |||
+ | |||
+ | This SQL creates triggers on the TransHeader, | ||
+ | |||
+ | |||
+ | |||
+ | ====== Risk of Data Corruption if Run Improperly ====== | ||
+ | |||
+ | |||
+ | |||
+ | There is no risk of data corruption, but the ErrorTable could grow unnecessarily large if not periodically deleted. | ||
+ | |||
+ | |||
+ | |||
+ | ====== SQL ====== | ||
+ | |||
+ | |||
+ | |||
+ | ===== Step 1 - Create the ErrorTable to Store errors ===== | ||
+ | |||
+ | |||
+ | |||
+ | <code sql> | ||
+ | CREATE TABLE [dbo].[ErrorTable]( | ||
+ | [TableName] [varchar](25) NOT NULL, | ||
+ | [ID] [int] NOT NULL, | ||
+ | [ClassTypeID] [int] NOT NULL, | ||
+ | [ModifiedByUser] [nvarchar](25) NULL, | ||
+ | [ModifiedByComputer] [nvarchar](25) NULL, | ||
+ | [ModifiedDate] [datetime] NOT NULL, | ||
+ | [OldSeqID] [int] NOT NULL, | ||
+ | [NewSeqID] [int] NOT NULL | ||
+ | | ||
+ | ( | ||
+ | [TableName], | ||
+ | )WITH (PAD_INDEX | ||
+ | ) ON [PRIMARY] | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Step 2 - Create the Triggers on the appropriate tables ===== | ||
+ | |||
+ | |||
+ | |||
+ | In general, only the primary tables that are edited are monitored by this query. It could easily be extended, but the value of that would be limited except in special circumstances. | ||
+ | |||
+ | |||
+ | |||
+ | <code sql> | ||
+ | -- ============================================= | ||
+ | -- Author: | ||
+ | -- Create date: 9/14/2012 | ||
+ | -- Description: | ||
+ | -- ============================================= | ||
+ | CREATE TRIGGER dbo.TransHeader_SeqIDErrors | ||
+ | | ||
+ | AFTER UPDATE | ||
+ | AS | ||
+ | BEGIN | ||
+ | -- SET NOCOUNT ON added to prevent extra result sets from | ||
+ | -- interfering with SELECT statements. | ||
+ | SET NOCOUNT ON; | ||
+ | -- Insert statements for trigger here | ||
+ | | ||
+ | | ||
+ | Inserted.ID, | ||
+ | Inserted.ClassTypeID, | ||
+ | Inserted.ModifiedByUser, | ||
+ | Inserted.ModifiedByComputer, | ||
+ | GETDATE() as [ModifiedDate], | ||
+ | Deleted.SeqID as OldSeqID, | ||
+ | Inserted.SeqID as NewSeqID | ||
+ | from Inserted join Deleted on Inserted.ID = Deleted.ID | ||
+ | where Inserted.SeqID | ||