Monday, February 4, 2008

Change a T-SQL variable in an 'update' statement

declare @message varchar(1024)
set @message = 'New message'

update MyTable
set Column1 = 'New Value',
Column2 = @message,
@message = 'Message updated successfully'
where [Some condition]

select @message

If one or more records have been updated with the update statement, the @message variable will contain "Message updated successfully".
If no record was updated the variable will contain the original text "New message".