At times you may need to UPDATE a row from a Table and at the same time you would also want to get the column value from the same SQL UPDATE query. Following is a solution using SQL OUTPUT clause .
SQL OUTPUT clause :
The OUTPUT Clause from SQL server provides a very useful feature with INSERT, UPDATE & DELETE query statement.This clause can be used with any of the above query statement(INSERT, UPDATE & DELETE) to identify the effected row. Following are the examples
|
Example-I (OUTPUT clause with INSERT)
INSERT
Employee ( EmployeeID, EmployeeName) OUTPUT Inserted.EmployeeID, Inserted.EmployeeName VALUES (11, ‘MFSEmployee’);
Example-II (OUTPUT clause with UPDATE)
UPDATE
Employee SET EmployeeName = ‘MFSEmployeeOne’ OUTPUT Inserted.EmployeeID, Inserted.EmployeeName
WHERE EmployeeID = 11; Example-III (OUTPUT clause with DELETE)
DELETE FROM
Employee OUTPUT Deleted.EmployeeID, Deleted.EmployeeName
WHERE EmployeeID = 11; |