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

x
?
Solved

SET NOCOUNT OFF - how to capture returned messages

Posted on 2009-05-09
6
Medium Priority
?
572 Views
Last Modified: 2012-06-27
Suppose I SET NOCOUNT OFF in a stored procedure; I know it's not recommended, but just suppose that for a moment.

The stored procedure performs numerous deletes, updates, inserts. After each of those a message is returned to the caller (in this case ADO.NET) containing the number of records affected.

How do I capture, in ADO.NET, such returned messages (a large number of such messages) ??
0
Comment
Question by:campinam
6 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1200 total points
ID: 24346650
you can use Output parameters to return the rows affected

create proc sample
@i int ,
@name varchar(10) ,
@rowsAffected1 int output,
@RowsAffected2 int output
as
SET NOCOUT ON

INSERT into....VALUES (... )
SET @RowsAffected1 = @@ROWCOUNT

UPDATE SomeTabel ...
SET @RowsAffected2 = @@ROWCOUNT
GO
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24347000
I agree with that method being the best!
0
 

Author Comment

by:campinam
ID: 24347004
Thank you!
Can I capture the same info (rows affected) using SET NOCOUNT OFF and messages it returns (this was the point of my question) - how do I capture messages caused by SET NOCOUNT OFF (maybe that's not possible??)
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 24347984
from what I know, that is indeed not possibly directly.
anyhow, as the parameter method is much easier to port to another database system, I would go that method anyway...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24348881
If you have SET NOCOUNT OFF you can use ExecteNonQuery to get the value, however it will only be the last one.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 400 total points
ID: 24348900
And it would help if I could type.  It should be ExecuteNonQuery.  :)

I should have also stated that this only applies to INSERT, UPDATE and DELETE.  All other statements return -1.

And it should be clarified (in case it was not obvious) that if you have SET NOCOUNT ON then ExecuteNonQuery will not reurn any value.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

886 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