sql server - Trigger with specific value? -
i've read questions/answers here on stack couldn't find need.
let's have following table:
col0 col1 col2 col3 col4 ---- ---- ---- ---- ---- 8 1 b c 8 2 b c 8 3 b c 9 1 b c 9 2 b c
and software does:
insert testtable ([col0],[col1],[col2],[col3],[col4]) values ('8','4','a','b','c')
how can create trigger pseudo-code:
on insert when col1 = '4' delete existing rows col0 same (8 in case) ** except new row i've added **
it sounds more appropriate perform in instead of trigger. if goal assume: when new row inserted col0 = 8, want delete other rows same key, yes?
create trigger dbo.testtable_instead_insert on dbo.testtable instead of insert begin set nocount on; delete t dbo.testtable t inner join inserted on t.col0 = i.col0 i.col1 = '4'; insert dbo.testtable(col0,col1,col2,col3,col4) select col0,col1,col2,col3,col4 inserted; end go
the question rule want follow if insert attempts add multiple rows same col0 value? imagine insert statement is:
insert dbo.testtable ([col0],[col1],[col2],[col3],[col4]) select '8','4','a','b','c' union select '8','3','c','d','e';
Comments
Post a Comment