5

I have a stored procedure that contains like 10 different INSERTS, is it possible to return the COUNT of the rows affected on each INSERT to ASP.NET c# page so i can display Stored Procedure process for the client viewing that ASP.NET page?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Dror
  • 1,262
  • 3
  • 21
  • 34

3 Answers3

3

You need to use following command in the start of your stored procedure:

SET NOCOUNT OFF

In this case SQL server will send text messages ("X rows affected" ) to client in real time after each INSERT/UPDATE. So all you need is to read these messages in your software.

Here is my answer how to do it in Delphi for BACKUP MS SQL command. Sorry I've not enough knowledge in C# but I guess you can do it in C# with SqlCommand class.

Community
  • 1
  • 1
valex
  • 23,966
  • 7
  • 43
  • 60
  • Yes! All these years later this solved it for me. so easy. return @@RowCount in stored procedure and int count= cmd.ExecuteNonQuery() gave me correct records affected. My guess is I didn't even need to put the return @@RowCount in the stored procedure. – JustJohn Jul 06 '19 at 22:06
2

After every Inserts, use @@ROWCOUNT, then get the value by query.

Returns the number of rows affected by the last statement. If the number of rows is more than 2 billion, use ROWCOUNT_BIG.

Sample:

USE AdventureWorks2012;
GO
UPDATE HumanResources.Employee 
SET JobTitle = N'Executive'
WHERE NationalIDNumber = 123456789
IF @@ROWCOUNT = 0
PRINT 'Warning: No rows were updated';
GO

Edit: How you can get @@rowcount with multiple query? Here's an example:

DECLARE @t int
DECLARE @t2 int
SELECT * from table1
SELECT @t=@@ROWCOUNT
SELECT * from table2
SELECT @t2=@@ROWCOUNT
SELECT @t,@t2'
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • If i have like 10 UPDATES how do i get the @@ROWCOUNT on each update to display in the asp.net page? or how do i "get the value by query."? – Dror Dec 05 '12 at 10:25
  • That's an option but it wont show me the proccess of the SP in "real time" , just when it finishes.. – Dror Dec 05 '12 at 10:39
2

On the server side send the message to the client using RAISERROR function with severity 10 (severity higher than 10 causes exception that breaks procedure execution, i.e. transfers execution to the CATCH block, if there is one). In the following example I haven't added error number, so the default error number of 50000 will be used by RAISERROR function. Here is the example:

DECLARE @count INT = 0
DECLARE @infoMessage VARCHAR(1000) = ''

-- INSERT

SET @count = @@ROWCOUNT
SET @infoMessage = 'Number of rows affected ' + CAST(@count AS VARCHAR(10))
RAISERROR(@infoMessage, 10, 0) WITH NOWAIT

-- another INSERT

SET @count = @@ROWCOUNT
SET @infoMessage = 'Number of rows affected ' + CAST(@count AS VARCHAR(10))
RAISERROR(@infoMessage, 10, 0) WITH NOWAIT

On the client side, set the appropriate event handlers, here is an example:

using (SqlConnection conn = new SqlConnection(...))
{
    conn.FireInfoMessageEventOnUserErrors = true;
    conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage);
    using (SqlCommand comm = new SqlCommand("dbo.sp1", conn) 
           { CommandType = CommandType.StoredProcedure })
    {
        conn.Open(); 
        comm.ExecuteNonQuery();
    }
}

static void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
    // Process received message
}
Ivan Golović
  • 8,732
  • 3
  • 25
  • 31
  • Of course, there is another task for you since you're implementing this into a web page. And that is how to send progress messages that arrive asynchronously (for example, every couple of seconds) from SQL Server to your web server and finally need to be sent to the browser. For that i recommend SignalR. – Ivan Golović Dec 05 '12 at 11:52
  • .. every time "RAISERROR" is called in SP will it trigger the event- conn_InfoMessage in code? – Dror Dec 05 '12 at 12:20
  • by the way if the answer is YES. i can use UPDATE PANEL (ajax) i believe to display the messages. – Dror Dec 05 '12 at 12:21
  • Yes, that's the idea. You can first take this code, adjust it to your needs and create a demo as a console application, to make sure it is working in a simple environment such as console app. Than you have to resolve the whole asynchronicity issue in the web app. Maybe it is possible with `UpdatePanel` (that could include polling the server to refresh message) but I believe this would be more complicated (and possibly less reliable) than using frameworks such as SignalR. – Ivan Golović Dec 05 '12 at 12:31