Return to Snippet

Revision: 49452
at July 22, 2011 01:38 by Whiteagle


Updated Code
if (SELECT COUNT(*) FROM YourTable WHERE YourVar = @YourValue) = 0
	BEGIN
		INSERT INTO YourTable(YourVar) VALUES(@YourVar)
	END
else
	BEGIN
		UPDATE YourTable
			   SET YourVar = @YourValue
			   WHERE YourVar = @YourValue
END

Revision: 49451
at July 22, 2011 01:33 by Whiteagle


Initial Code
if (SELECT COUNT(*) FROM YourTable WHERE YourVar = @YourValue) = 0
	BEGIN
		INSERT INTO YourTable(YourVar) VALUES(@YourVar)
				END
else
	BEGIN
		UPDATE YourTable
			   SET YourVar = @YourValue
			   WHERE YourVar = @YourValue
END

Initial URL
http://www.digitalwks.com/blogs/diogo-raminhos/sql-series-insertupdate-in-one-stored-procedure/

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

Initial Title
Insert/Update in one stored procedure

Initial Tags
sql, update

Initial Language
SQL