sql server - trigger with a select condition -
i want create trigger doesn't perform insert if insert column 2 exists in table , value column 1 in table different value inserted column 1. i've done
create trigger tr1 on dbo.table after insert, update begin declare @col1 varchar(20) declare @col2 varchar(20) if ( exists( select column2 table column2='@col2' ) , @col1 <> select column1 table column2='@col2' ) begin raiserror('error', 16, 1); rollback end end
i'm not sure i've understood tryng easy way check existing values triggers use transaction tables.
- inserted update , insert
- deleted delete
1.
create table some_table(col1 int , col2 int) go
2.
create trigger tr1 on dbo.some_table after insert, update begin declare @cnt int select @cnt = count(*) some_table t inner join inserted on t.col1 = i.col1 , t.col2 = i.col2 if isnull(@cnt,0) > 1 begin raiserror('error', 16, 1); rollback end end
3.
insert some_table(col1, col2) values(1,2) go insert some_table(col1, col2) values(2,3) go --here trigger crash insert some_table(col1, col2) values(1,2) go
i prefer unique index approach :)
Comments
Post a Comment