Language: SQL
Detecting changed rows
1: create table some_base_table ( 2: batch_id int not null primary key clustered, 3: batch_name varchar(250) not null, 4: some_parm int null, 5: some_parm2 int null 6: ) 7: 8: insert some_base_table ( batch_id, batch_name, some_parm, some_parm2 ) 9: values( 1, 'hello fred', 1, 2 ) 10: insert some_base_table ( batch_id, batch_name, some_parm, some_parm2 ) 11: values( 2, 'hello fred', 1, 2 ) 12: insert some_base_table ( batch_id, batch_name, some_parm, some_parm2 ) 13: values( 3, 'hello fred', 1, 2 ) 14: insert some_base_table ( batch_id, batch_name, some_parm, some_parm2 ) 15: values( 4, 'hello fred', 1, 2 ) 16: insert some_base_table ( batch_id, batch_name, some_parm, some_parm2 ) 17: values( 5, 'hello fred', 1, 2 ) 18: insert some_base_table ( batch_id, batch_name, some_parm, some_parm2 ) 19: values( 6, 'hello fred', 1, 2 ) 20: 21: create trigger trg_some_base_table_check_for_changes on some_base_table for update 22: as 23: begin 24: select batch_id 25: from ( 26: select distinct batch_id, hash_combined = hashbytes( 'sha1', combined ) 27: from ( select batch_id, 28: combined =( select batch_id, batch_name, some_parm, some_parm2 29: from deleted c -- need old values 30: where c.batch_id = d.batch_id 31: for xml path( '' ) ) 32: from deleted d 33: union all 34: select batch_id, 35: combined =( select batch_id, batch_name, some_parm, some_parm2 36: from some_base_table c -- need current values (could use inserted ere) 37: where c.batch_id = d.batch_id 38: for xml path( '' ) ) 39: from deleted d 40: ) as r 41: ) as c 42: group by batch_id 43: having count(*) > 1 44: end 45: go 46: 47: -- test 48: update some_base_table 49: set batch_name = lower( batch_name ) 50: where batch_id = 2 51: 52: update some_base_table 53: set batch_name = lower( batch_name )
Tags:
Description:
Good point. To be tested. How about performance?
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

