Posted By

Whiteagle on 07/22/11


sql insert stored update procedures

Versions (?)

Insert/Update in one stored procedure

 / Published in: SQL


To start my new post area I will share a simple stored procedure that will check if a record exists, if so update it, otherwise create it; Simple hum?! Just Update it to your needs :)

  1. IF (SELECT COUNT(*) FROM YourTable WHERE YourVar = @YourValue) = 0
  2. BEGIN
  3. INSERT INTO YourTable(YourVar) VALUES(@YourVar)
  4. END
  5. else
  6. BEGIN
  7. UPDATE YourTable
  8. SET YourVar = @YourValue
  9. WHERE YourVar = @YourValue
  10. END

Report this snippet  

You need to login to post a comment.