Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I see all recs that were affected in SQL Update Statement

Posted on 2011-09-07
3
Medium Priority
?
224 Views
Last Modified: 2012-06-27
In my ASP.NET app, I have a function that allows users to update the status of certain records.  After the Update, I want to populate a gridview that shows the user all of the records that were updated.  What's a good way to do that based off my current Update code (below).  Thanks -
string strSql = "Update metadata set Status = 'Active' where Status = 'XL_Import'";
        try
        {
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSql.ToString();
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            string errorMsg = "Error in Updation";
            errorMsg += ex.Message;
            throw new Exception(errorMsg);
        }
        finally
        {
            con.Close();
        }

Open in new window

0
Comment
Question by:JT_SIRO
3 Comments
 
LVL 2

Accepted Solution

by:
AJRDev earned 500 total points
ID: 36499503
ExecuteNonQuery(), or any of the other ADO.NET SQL methods, can only ever return to you the number of records affected - never which records!

The only way to do this is with logic at the SQL level. You need to write a stored procedure that creates a temporary table, populates this table with the affected record id's, and returns that to your application.
0
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36501686
@@RowCount will give you the number of records affected by a SQL Statement.

The @@RowCount works only if you issue it immediately afterwards. So if you are trapping errors, you have to do it on the same line. If you split it up, you will miss out on whichever one you put second.

SELECT @NumRowsChanged = @@ROWCOUNT

Write Stored procedure for this. hope this will work.

Thanks
0
 
LVL 7

Assisted Solution

by:gopaltayde
gopaltayde earned 500 total points
ID: 36516107
Create a copy of metadata table, with some more column like Operation, OprationDate etc, Write an after trigger on metadata table which will populate this newly created table.
A sample example -
http://weblogs.asp.net/jgalloway/archive/2008/01/27/adding-simple-trigger-based-auditing-to-your-sql-server-database.aspx
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question