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
 stored procedure to prevent duplication

Author  Topic 

sparrow37
Posting Yak Master

148 Posts

Posted - 2009-08-07 : 13:05:28
Hi all,

I want to create a stored procedure, which will check if a username is present in db then return error or some code( like 0) else insert the record. And there will be another stored procedure which will check that same username is not present in db for another user except current one ( whose data is being passed to sp) if it exist then return error else update record. Can all these things (insert, update and checking for duplication) be done in one sp?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-07 : 13:46:05
>>Can all these things (insert, update and checking for duplication) be done in one sp?
yes

If you create a unique constraint on that column then won't that achieve your objective. You'll get successfull inserts and updates and an error if you attempt to insert a duplicate or update to an existing value.

Be One with the Optimizer
TG
Go to Top of Page

vedjha
Posting Yak Master

228 Posts

Posted - 2009-08-07 : 13:58:56

create proc [prcName] @sid varchar(9)
as
begin
if exists (select * from members where vuser=@sid)
begin

if exists (select vuser from members where vuser=@sid group by vuser having count(vuser) >1 )
begin

print 'Duplicate Data'


end

else

begin

/* Process insert, Update, Delete */

end

end

else

begin

print 'Please check your ID'

end

end


Ved Prakash Jha
Go to Top of Page
   

- Advertisement -