HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

See actual results of SQL UPDATE command

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
updateactualsqlseeresultscommand

Problem

Is there a way to get the actual results of SQL UPDATE command (ie the table rows themselves), not just get the number of rows (other than by converting the UPDATE command to a SELECT)? I'm using mainly Postgres and Informix, but I'd be interested to hear about other database systems that have this feature, or perhaps a particular client that supports this.

Solution

Here's how you'd do something like that in SQL Server. Because an UPDATE is essentially old data and new data, SQL Server exposes the deleted and inserted dynamic tables. You can work with them to get what the data looked like prior to the UPDATE, and after the UPDATE:

update HumanResources.Department
set GroupName = 'Testing Group Name'
output
    deleted.DepartmentID as Old_DepartmentID,
    inserted.DepartmentID as New_DepartmentID,
    deleted.Name as Old_Name,
    inserted.Name as New_Name,
    deleted.GroupName as Old_GroupName,
    inserted.GroupName as New_GroupName
where DepartmentID > 12

Code Snippets

update HumanResources.Department
set GroupName = 'Testing Group Name'
output
    deleted.DepartmentID as Old_DepartmentID,
    inserted.DepartmentID as New_DepartmentID,
    deleted.Name as Old_Name,
    inserted.Name as New_Name,
    deleted.GroupName as Old_GroupName,
    inserted.GroupName as New_GroupName
where DepartmentID > 12

Context

StackExchange Database Administrators Q#16382, answer score: 3

Revisions (0)

No revisions yet.