Language: SQL
RE: Detecting changed rows in a trigger using HashBytes and without EVENTDATA and OR's
1: CREATE TABLE SomeBaseTable 2: ( 3: BatchId int NOT NULL PRIMARY KEY, 4: BatchName varchar(250) NOT NULL, 5: AnIntParameter int NULL, 6: AnotherIntParameter int NULL 7: ) 8: GO 9: 10: INSERT INTO SomeBaseTable 11: ( 12: BatchId, 13: BatchName, 14: AnIntParameter, 15: AnotherIntParameter 16: ) 17: VALUES 18: ( 19: 1, 20: 'hello fred', 21: 1, 22: 2 23: ) 24: 25: INSERT INTO SomeBaseTable 26: ( 27: BatchId, 28: BatchName, 29: AnIntParameter, 30: AnotherIntParameter 31: ) 32: VALUES 33: ( 34: 2, 35: 'hello fred', 36: 1, 37: 2 38: ) 39: 40: INSERT INTO SomeBaseTable 41: ( 42: BatchId, 43: BatchName, 44: AnIntParameter, 45: AnotherIntParameter 46: ) 47: VALUES 48: ( 49: 3, 50: 'hello fred', 51: 1, 52: 2 53: ) 54: 55: INSERT INTO SomeBaseTable 56: ( 57: BatchId, 58: BatchName, 59: AnIntParameter, 60: AnotherIntParameter 61: ) 62: VALUES 63: ( 64: 4, 65: 'hello fred', 66: 1, 67: 2 68: ) 69: 70: INSERT INTO SomeBaseTable 71: ( 72: BatchId, 73: BatchName, 74: AnIntParameter, 75: AnotherIntParameter 76: ) 77: VALUES 78: ( 79: 5, 80: 'hello fred', 81: 1, 82: 2 83: ) 84: 85: INSERT INTO SomeBaseTable 86: ( 87: BatchId, 88: BatchName, 89: AnIntParameter, 90: AnotherIntParameter 91: ) 92: VALUES 93: ( 94: 6, 95: 'hello fred', 96: 1, 97: 2 98: ) 99: GO 100: 101: CREATE TRIGGER SomeBaseTableOnUpdate ON SomeBaseTable 102: FOR UPDATE 103: AS 104: BEGIN 105: SELECT InsertedDerivedTable.BatchId, 106: CASE WHEN DeletedDerivedTable.[BINARY_CHECKSUM] IS NOT NULL AND 107: DeletedDerivedTable.BatchName <> InsertedDerivedTable.BatchName COLLATE SQL_Latin1_General_CP1_CS_AS 108: THEN InsertedDerivedTable.BatchName 109: ELSE NULL 110: END AS BatchName, 111: CASE WHEN DeletedDerivedTable.[BINARY_CHECKSUM] IS NOT NULL AND 112: DeletedDerivedTable.AnIntParameter <> InsertedDerivedTable.AnIntParameter 113: THEN InsertedDerivedTable.AnIntParameter 114: ELSE NULL 115: END AS AnIntParameter, 116: CASE WHEN DeletedDerivedTable.[BINARY_CHECKSUM] IS NOT NULL AND 117: DeletedDerivedTable.AnotherIntParameter <> InsertedDerivedTable.AnotherIntParameter 118: THEN InsertedDerivedTable.AnotherIntParameter 119: ELSE NULL 120: END AS AnotherIntParameter 121: FROM ( 122: SELECT *, BINARY_CHECKSUM(*) AS [BINARY_CHECKSUM] 123: FROM inserted 124: ) AS InsertedDerivedTable 125: LEFT OUTER JOIN 126: ( 127: SELECT *, 128: BINARY_CHECKSUM(*) AS [BINARY_CHECKSUM] 129: FROM deleted 130: ) AS DeletedDerivedTable 131: ON DeletedDerivedTable.BatchId = InsertedDerivedTable.BatchId 132: WHERE DeletedDerivedTable.[BINARY_CHECKSUM] IS NULL OR 133: DeletedDerivedTable.[BINARY_CHECKSUM] <> InsertedDerivedTable.[BINARY_CHECKSUM] 134: END 135: GO 136: 137: -- test 138: UPDATE SomeBaseTable 139: SET BatchName = UPPER(BatchName), 140: AnIntParameter = AnIntParameter + 2 141: 142: UPDATE SomeBaseTable 143: SET BatchName = LOWER(BatchName), 144: AnIntParameter = AnIntParameter - 2 145: 146: UPDATE SomeBaseTable 147: SET AnotherIntParameter = AnotherIntParameter - 4 148: 149: UPDATE SomeBaseTable 150: SET AnotherIntParameter = AnotherIntParameter + 4 151: 152: UPDATE SomeBaseTable 153: SET BatchId = 16 154: WHERE BatchId = 6 155: 156: UPDATE SomeBaseTable 157: SET BatchId = 6 158: WHERE BatchId = 16 159: GO
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search


BINARY_CHECKSUM will not I believe work for text and ntext columns where as the method the above technique stems from (http://sqlblogcasts.com/blogs/tonyrogerson/archive/2009/10/21/detecting-changed-rows-in-a-trigger-using-hashbytes-and-without-eventdata-and-or-s.aspx) uses the HashBytes function instead and FOR XML so you will get a full crap at the whip on changes and compatible columns.