Makis Makis - 28 days ago 4
ASP.NET (C#) Question

New value of a column after updating SQL Server

I have a

SqlCommand


UPDATE table SET intColumn = intColumn + 1 WHERE id = @id


Is there a way to get the new value of
intColumn
in the same
SqlCommand
?

Something like

UPDATE table
SET intColumn = intColumn + 1
WHERE id = @id;
SELECT intColumn


with
ExecuteScalar
?

for example:

Dim x as int32 = sqlCommand.ExecuteScalar("sql")


and the x is the new value of the
intColumn
?

Answer

The SQL query should be framed as below

    sqlCommand.ExecuteScalar("UPDATE table SET intColumn=intColumn+1 
                             OUTPUT INSERTED.intColumn
                             WHERE id=@id")