Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to write a stored procedure to update existing and add a new item
Posted
Comments
navin ks 9-Apr-13 7:51am    
shall i come and write
ZurdoDev 9-Apr-13 7:52am    
OK. Where are you stuck?
[no name] 9-Apr-13 7:55am    
Googling "how to write a stored procedure" returned over 5 million results. Are you saying that you went through all of those links and nothing helped you at all?

1 solution

This might be what you are looking for. It is available from plenty of places such as Google.

SQL
IF EXISTS (SELECT * FROM [dbo].[Table] WHERE [uid]=@uid) BEGIN
    --update existing record
    UPDATE [dbo].[Table] SET [Field1]=@Parameter1, [Field2]=@Parameter2 WHERE [uid]=@uid END
ELSE
BEGIN
    --insert new record
    INSERT INTO [dbo].[Table] ([Field1], [Field2])
    VALUES (@Parameter1, @Parameter2) SET @uid=SCOPE_IDENTITY() END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900