Tuesday, March 20, 2012

Replace

I know there is a function in MySQL where you can use "Replace" to update a row if one already exists and if the row does not exist it will insert a new row. Can this be done in MSSQL also? Thanksif exists (select * from tbl where ...)
update tbl set ...
else
insert tbl ...

update tbl set ...
select @.error = @.@.error, @.rowcount = @.@.rowcount
if @.@.error = 0 and @.rowcount = 0
insert tbl ...

I include the @.@.error in the second method to show that if you check one then you will lose the other so have to save them both in one statement.

No comments:

Post a Comment