Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Trigger code

Author  Topic 

shajimanjeri
Posting Yak Master

179 Posts

Posted - 2008-06-14 : 05:35:45
How can I write a trigger for the below issue,
I have table named Subscribers with fields Name (varchar), MobileNumber (varchar), status (char). Here if I want to insert a record into this table then first I need to check whether this mobile number exist or not in this table. If it is not exist then this record should be insert and if it is exist then this record should not be insert in to this table.

How can I do a trigger for this one?

with regards
Shaji

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-06-14 : 06:13:00
for this u can use SP rather than Trigger as mobileno is parameter

some thing like this will do u r job

if not exists ( select 1 from tbl where mobileno = @mobileno )
insert
else
return
Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2008-06-14 : 07:00:17
If this is something your doing frequently and the table is large, make sure you have some form of index on mobile number of your performance is going to be horrible.

Mike
"oh, that monkey is going to pay"
Go to Top of Page

shajimanjeri
Posting Yak Master

179 Posts

Posted - 2008-06-14 : 07:04:40
Can you give me a real example on this please?
Go to Top of Page

shajimanjeri
Posting Yak Master

179 Posts

Posted - 2008-06-14 : 07:11:35
or is there any way to avoid duplicate entry of mobile number in this table?
Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2008-06-14 : 07:43:49
If you want to absolutely prevent duplication you could make the mobile # the primary key or add a unique constraint to the field. If an attempt is made to insert a duplicate it will fail which will need to be trapped and handled.

Mike
"oh, that monkey is going to pay"
Go to Top of Page
   

- Advertisement -