Friday, March 23, 2012

Insert Trigger How to work

Hi All!

I have a table which has fields |person_id| AND |fullname|

I would like to create and insert trigger so that no duplicate names shoudl be inserted, I tryed Following but am geeting confused Where I am going Wrong

I need to take an Input from user to this trigger as well how is it possiible??

create trigger ins_per
on persons
for insert as
Declare @.p_name varchar(45)
select * from persons where full_name=@.p_name
if @.@.rowcount>0
Begin
Print
'Sorry'
ROLLBACK TRANSACTION
EndA unique constraint does the trick!

Alter table x add constraint constraint_name unique nonclustered (Column 1,Column 2)

The combination column1 + column2 must be unique.

Hope it helps!sql

No comments:

Post a Comment