Insert/Update in one stored procedure

/ Published in: SQL
Save to your folder(s)

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 :)

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.