I am writing a request that updates a userβs vote ( ForumVotes ) for a forum post ( ForumPosts ). Users can vote up or down (the vote will be 1 or -1). This question is specific for changing a user's vote, so a voice recording already exists in the ForumVotes table.
The ForumPosts table stores the total score for each post, so I need to synchronize this field. To recount the total score, I need to first subtract the old vote before adding a new vote, so I need to get the old vote before updating the user's voice recording.
I know I can do this with two queries, but I wonder if it is possible (in SQL Server 2008) that UPDATE returns the column value before performing the update?
Here is an example:
TABLE ForumPosts ( postID bigint, score int, ... etc ) -- existing vote is in this table: TABLE ForumVotes ( postFK bigint, userFK bigint, score int )
Simple request to update user voice
UPDATE ForumVotes SET score = @newVote WHERE postFK = @postID AND userFK = @userID
Can I change this request to return the old account before the upgrade?
Redtopia
source share