Makis
Makis

Reputation: 337

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 ?

Upvotes: 1

Views: 410

Answers (1)

praveen
praveen

Reputation: 12271

The SQL query should be framed as below

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

Upvotes: 1

Related Questions