dev-exchange.com logo

dev-exchange.com latest topics RSS feed

Book mark: Add http://www.dev-exchange.com/cms_view_article.php?aid=34&start=0&sid=68a8832a8eefb73158d7a3ef9209c8a6 to your delicious account Add http://www.dev-exchange.com/cms_view_article.php?aid=34&start=0&sid=68a8832a8eefb73158d7a3ef9209c8a6 to your digg account Add http://www.dev-exchange.com/cms_view_article.php?aid=34&start=0&sid=68a8832a8eefb73158d7a3ef9209c8a6 to your blinklist account Add http://www.dev-exchange.com/cms_view_article.php?aid=34&start=0&sid=68a8832a8eefb73158d7a3ef9209c8a6 to your reddit account Add http://www.dev-exchange.com/cms_view_article.php?aid=34&start=0&sid=68a8832a8eefb73158d7a3ef9209c8a6 to Dzone Add http://www.dev-exchange.com/cms_view_article.php?aid=34&start=0&sid=68a8832a8eefb73158d7a3ef9209c8a6 to your furl account Add http://www.dev-exchange.com/cms_view_article.php?aid=34&start=0&sid=68a8832a8eefb73158d7a3ef9209c8a6 to your stumble account Add http://www.dev-exchange.com/cms_view_article.php?aid=34&start=0&sid=68a8832a8eefb73158d7a3ef9209c8a6 to your Yahoo myweb account
Article Menu
Google
ColdFusion MX
PHP
ASP
ASP.Net
XML-XSLT
JavaScript
SQL Server
AJAX
Android
 


 
Article
Kishore
Contributing Member
Contributing Member

Articles: 10 Comments: 3
 Posted: Tue Jul 08, 2008 12:07 pm

SQL server has a nice OUTPUT clause which can be used for retrieving the deleted/updated/inserted information from Data Manipulation Language (DML) commands. This article shows few examples on how to fetch the affected rows after executing INSERT,UPDATE or DELETE commands.


First of all create a temporary table variable for holding the affected rows information.

Code:
Declare @tempAffectedData table (Sno,Name);



Sno and Name are the two columns which should be same as the test tables in examples below. You should change the columns when you try with your queries.

1. INSERTED Rows

When inserting into a table we can find newly inserted rows using INSERTED variable.

Example

Code:
INSERT TestTable
    OUTPUT INSERTED.Sno, INSERTED.Name
        INTO @tempAffectedData
VALUES (1, 'Alexander');

-- inserted data
select * from @tempAffectedData



2. DELETED

When deleting the records from a table OUTPUT clause can help to find the deleted rows


Code:
   
DELETE  TestTable
    OUTPUT DELETED.* INTO @tempAffectedData;

-- deleted data
select * from @tempAffectedData



3. DELETED and INSERTED for finding updated

Now we can use both INSERTED and DELETED variables to find the updated rows in a table.

Code:

-- new table variable to catch the affected rows
Declare @tempaffectedRows table (Name,deletedname);

UPDATE TestTable
  SET NAME = 'Alexander Senr'     
OUTPUT INSERTED.Name,
       DELETED.Name   
INTO @tempaffectedRows
WHERE Sno=1;

-- updated rows
select * from @tempaffectedRows


OUTPUT clause can be really handy when you want to know details about the affected rows after a DML command.

Post your comments on this article.

Thanks
Rating: 0.00/5.00 [0]

Comments
aaaa78101
Author


Joined: 01 Jun 2011
Articles: 0
Comments: 3
 Posted: Wed Jun 01, 2011 7:45 am  Post subject:

If you want your trigger to report all affected rows, you need to write a loop

USE FyiCenterData;
GO
select * from fyi_users;

-- reporting the first affected row only
UPDATE fyi_users SET email=name;
GO
Email changed from NULL to John King
(5 row(s) affected)

-- reporting all affected rows
ALTER TRIGGER update_user ON fyi_users
AFTER UPDATE
AS
SELECT 'Email changed from '
+ ISNULL(d.email,'NULL')
+ ' to '
+ ISNULL(i.email,'NULL')
FROM INSERTED AS i, DELETED AS d
WHERE i.id = d.id;
GO

UPDATE fyi_users SET email=REVERSE(name);
GO
------------------------------------------------------
Email changed from Marc Kumar to ramuK craM
Email changed from Roy Bush to hsuB yoR
Email changed from Jack Gate to etaG kcaJ
Email changed from Nancy Greenberg to grebneerG ycnaN
Email changed from John King to gniK nhoJ
(5 row(s) affected)

(5 row(s) affected)






oracle Application development in ahmedabad
PHP development in ahmedabad
wordpress Development in ahmedabad
Oracle company in ahmedabad
Rating: 0.00/5.00 [0]
 

Page 1 of 1
All times are GMT

Jump to:  
You cannot post articles in this chapter
You cannot edit your articles in this chapter
You cannot delete your articles in this chapter
You cannot rate articles in this chapter

You cannot post comments in this chapter
You cannot edit your comments in this chapter
You cannot delete your comments in this chapter
You cannot rate comments in this chapter


© 2008 dev-exchange.com
Powered by phpBB. Theme DEVPPL.