开发者

SQL Server Update With TimeStamp and OUTPUT

开发者 https://www.devze.com 2023-03-06 11:28 出处:网络
I have a table that has 5 columns (ID, Value, TimeStamp, LastModified, ModifiedBy) I want to create a stored procedures that does the following op开发者_Python百科erations.

I have a table that has 5 columns (ID, Value, TimeStamp, LastModified, ModifiedBy)

I want to create a stored procedures that does the following op开发者_Python百科erations.

1) Update the value if TimeStamp matches

2) Return new TimeStamp, LastModified datetime

3) If TimeStamp does not match return Value, TimeStamp, LastModified and ModifiedBy

I know I can do this by using multile statements within my stored procedure but would this be possible to do it in one statement using the OUTPUT clause

e.g.

UPDATE "table" SET "value" = "new value"

OUPUT Inserted.Value, Inserted.TimeStamp, Inserted.LastModified, Inserted.ModifiedBy

WHERE "ID" = "ID" AND "TimeStamp" = "TimeStamp"

This works great as long as the update happens if the update does not happen the return values are all blank.


Once you have called your update sproc, you can check the @@ROWCOUNT variable to see if any rows were actually updated

If @@ROWCOUNT <> 0
    -- Successful write
ELSE
    -- Failed to update as the timestamp didn't match
0

精彩评论

暂无评论...
验证码 换一张
取 消