Noob question here, every time I change a certain record in an SQL Server 2008 R2 table, I want to increment a RevisionId record; to do so, I'm using the following syntax:
UPDATE TheTable
SET RevisionId=(SELECT RevisionId
FROM TheTable
WHERE Id=@id) + 1
WHERE Id=@id;
Btw, I'm going to put this into a trigger so that this happens automagically, but while this code works, it feels pretty clunky—any cleaner way to do this?
You don't need the inner select:
UPDATE TheTable SET RevisionId = RevisionId + 1 WHERE Id=@id
This is a SQL idiom for incrementing a field:
UPDATE TheTable
SET RevisionId = RevisionId + 1
WHERE Id=@id;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With