Solved

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

Posted on 2011-09-07
3
212 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 125 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 125 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now