Verifying updated values in SQL SERVER

Posted on Updated on

In most cases after table is updated we fire a select statement to check whether the value is updated correctly or not.

We can accomplish the same task by using OUTPUT clause in SQL server. Using Output clause we can verify the original value and updated values.

Here I’ll update column  MaritalStatus of employee table of AdventureWorks2008 database.

Before doing update we’ll check the values of MaritalStatus Employee table.

SELECT   BusinessEntityID
       , MaritalStatus

FROM  [HumanResources].[Employee]

WHERE BusinessEntityID IN ( 1,2,4)


BusinessEntityID MaritalStatus
1 S
2 S
4 S


In below query we are updating MaritalStatus from ‘S’ to ‘M’

UPDATE [HumanResources].[Employee]

SET MaritalStatus ='M'

OUTPUT  deleted.BusinessEntityID
       ,deleted.MaritalStatus AS BeforeUpdateMaritalStatus
       ,inserted.MaritalStatus AS AfterUpdateMaritalStatus
WHERE  MaritalStatus='S'
AND BusinessEntityID IN ( 1,2,4)


After executing the query we’ll get below  output which shows values before and after update.

BusinessEntityID BeforeUpdateMaritalStatus AfterUpdateMaritalStatus
1 S M
2 S M
4 S M

We again cross verify by running below query.

SELECT  BusinessEntityID
FROM  [HumanResources].[Employee]
WHERE BusinessEntityID IN ( 1,2,4)
BusinessEntityID MaritalStatus
1 M
2 M
4 M

So now we can use output clause directly instead of using select statements to verify updated values and original values.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s