Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Please someone tell how to calculate the records that have been deleted from a table. Please help me to do this in a stored procedure. One more thing I want to know is if I have to use trigger for that? I don't know the correct way and solution, please help me to solve this problem.
Posted
Updated 14-Feb-13 4:35am
v2
Comments
Sandeep Mewara 14-Feb-13 9:40am    
how to calculate the records which have deleted from table
Are you maintaining any record of deletion? If not, you cannot.
ZurdoDev 14-Feb-13 9:53am    
What do you mean calculate the records which are deleted? Do you mean get a count when you do the delete? Please explain more.
Chris Reynolds (UK) 14-Feb-13 10:00am    
Sounds like you need a trigger. There are plenty of examples around on the web. If you tell us what you want to do when a record is deleted we can offer more specific help.

As usually, there is no single correct solution, but one of the ways it could be achieved is a trigger. Trigger in a MSSQL has an access to inserted and deleted records referenced as @inserted and @deleted respectively.

Thus, as correctly mentions Chris Reynolds (BNY), one of the options could be the trigger which might call a specific stored procedure passing these records.

One of examples for such trigger could be found on a link below

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/e883cdbc-3b17-430e-823b-02ba800cffc9[^]

General info on @deleted @inserted in triggers on msdn

http://msdn.microsoft.com/en-us/library/ms191300.aspx[^]
 
Share this answer
 
declare a variable in your stored procedure like:
SQL
DECLARE @numberofrowsdeleted INT

use it like:
SQL
DELETE FROM YourTable WHERE conditionIsTrue
SET @numberofrowsdeleted = @@ROWCOUNT 

@numberofrowsdeleted will give you the number of deleted rows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900