473,769 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting rowcount from a stored procedure

I'm a database guy, so go easy on me here. :) How can I get the rowcount
of the affected rows of a SQL statement from a stored procedure call? I
know that "set nocount on" does not return the number of affected rows to
the client, so I would assume that "set nocount off" sends the number of
affected rows to the client, and therefore, is available programatically .
If so, how to get that rowcount?

Thanks,
Richard
Nov 15 '05 #1
4 23545
The SqlCommand.Exec uteNonQuery will return the rowcount for non queries. In
SELECT statements, well, just count how many rows you get :).
-mike
MVP

"Richard G" <ri*********@gt e.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
I'm a database guy, so go easy on me here. :) How can I get the rowcount
of the affected rows of a SQL statement from a stored procedure call? I
know that "set nocount on" does not return the number of affected rows to
the client, so I would assume that "set nocount off" sends the number of
affected rows to the client, and therefore, is available programatically .
If so, how to get that rowcount?

Thanks,
Richard

Nov 15 '05 #2
m
Mike-

int count = command.Execute NonQuery();

return ( ( count == -1 ) ? count : ((int)count / 2) );

Then why do I always have to halve the number of rows affected in order to
get an accurate result? Am I going about this wrong in some way? I
consistently get results that are double what my actual database tables
reflect, in both delete and bulk insert queries.

Thanks

------------------------------------------------------------

"Michael Giagnocavo [MVP]" <mg*******@Atre vido.net> wrote in message
news:eQ******** ******@TK2MSFTN GP12.phx.gbl...
The SqlCommand.Exec uteNonQuery will return the rowcount for non queries. In SELECT statements, well, just count how many rows you get :).
-mike
MVP

"Richard G" <ri*********@gt e.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
I'm a database guy, so go easy on me here. :) How can I get the rowcount of the affected rows of a SQL statement from a stored procedure call? I
know that "set nocount on" does not return the number of affected rows to the client, so I would assume that "set nocount off" sends the number of
affected rows to the client, and therefore, is available programatically . If so, how to get that rowcount?

Thanks,
Richard


Nov 15 '05 #3
Do you have any triggers? One way would be to try using the SQL Query
analyser and see what's going on. I believe you can do a SELECT @@ROWCOUNT
after your query.
-mike
MVP

"m" <sk****@speakea sy.net> wrote in message
news:uf******** ************@sp eakeasy.net...
Mike-

int count = command.Execute NonQuery();

return ( ( count == -1 ) ? count : ((int)count / 2) );

Then why do I always have to halve the number of rows affected in order to
get an accurate result? Am I going about this wrong in some way? I
consistently get results that are double what my actual database tables
reflect, in both delete and bulk insert queries.

Thanks

------------------------------------------------------------

"Michael Giagnocavo [MVP]" <mg*******@Atre vido.net> wrote in message
news:eQ******** ******@TK2MSFTN GP12.phx.gbl...
The SqlCommand.Exec uteNonQuery will return the rowcount for non queries.

In
SELECT statements, well, just count how many rows you get :).
-mike
MVP

"Richard G" <ri*********@gt e.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
I'm a database guy, so go easy on me here. :) How can I get the rowcount of the affected rows of a SQL statement from a stored procedure call? I know that "set nocount on" does not return the number of affected rows to the client, so I would assume that "set nocount off" sends the number of affected rows to the client, and therefore, is available programatically . If so, how to get that rowcount?

Thanks,
Richard



Nov 15 '05 #4
I ran an identical bulk insert query in Query Analyzer and got the correct
number of rows import, 1520. However, with code, I always get 3040 for some
reason. Do you have any ideas why I am getting double the rows affected on
bulk inserts? This has happened on a dev box and our live boxes(we are
currently halving num rows affected in order to get accurate results from
the function, but now I am trying again because I don't like that band-aid
approach :) )

Thanks!
M

Query Analyzer:
BULK INSERT residential FROM
'\\workskibbs\c $\databases\ari zona\residentia lExport.txt' WITH
(FIELDTERMINATO R = '|', ROWTERMINATOR=' \n')

C# Code Chunk:
private int importData( string tableName )

{

string query = "BULK INSERT " + tableName + " FROM '" + OutputDirectory +
tableName + ".txt' WITH (FIELDTERMINATO R = '|', ROWTERMINATOR=' \n')";
SqlConnection connection;

connection = new SqlConnection(c onnectionString );

SqlCommand command = new SqlCommand( query, connection );

try

{

connection.Open ();

command.Command Timeout = 6000;

int count = command.Execute NonQuery();

Console.WriteLi ne( "Imported " + count.ToString( ) + " rows into the " +
tableName + " table..." );
return count;

}

catch( Exception yy )

{

new ErrorLog( yy.ToString() );

return -1;

}

finally

{

if( connection != null )

if( connection.Stat e == ConnectionState .Open )

connection.Clos e();

connection.Disp ose();

if( command != null )

command.Dispose ();

}

}
"Michael Giagnocavo [MVP]" <mg*******@Atre vido.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Do you have any triggers? One way would be to try using the SQL Query
analyser and see what's going on. I believe you can do a SELECT @@ROWCOUNT after your query.
-mike
MVP

"m" <sk****@speakea sy.net> wrote in message
news:uf******** ************@sp eakeasy.net...
Mike-

int count = command.Execute NonQuery();

return ( ( count == -1 ) ? count : ((int)count / 2) );

Then why do I always have to halve the number of rows affected in order to
get an accurate result? Am I going about this wrong in some way? I
consistently get results that are double what my actual database tables
reflect, in both delete and bulk insert queries.

Thanks

------------------------------------------------------------

"Michael Giagnocavo [MVP]" <mg*******@Atre vido.net> wrote in message
news:eQ******** ******@TK2MSFTN GP12.phx.gbl...
The SqlCommand.Exec uteNonQuery will return the rowcount for non
queries. In
SELECT statements, well, just count how many rows you get :).
-mike
MVP

"Richard G" <ri*********@gt e.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
> I'm a database guy, so go easy on me here. :) How can I get the rowcount
> of the affected rows of a SQL statement from a stored procedure
call? I > know that "set nocount on" does not return the number of affected
rows to
> the client, so I would assume that "set nocount off" sends the
number of > affected rows to the client, and therefore, is available

programatically .
> If so, how to get that rowcount?
>
> Thanks,
> Richard
>
>



Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7294
by: Warren Wright | last post by:
Hi group, I have a select statement that if run against a 1 million record database directly in query analyzer takes less than 1 second. However, if I execute the select statement in a stored procedure instead, calling the stored proc from query analyzer, then it takes 12-17 seconds. Here is what I execute in Query Analyzer when bypassing the stored procedure:
3
2716
by: Cliff | last post by:
here's my stored procedure: CREATE PROCEDURE proc @id varchar(50),@pswd varchar(20),@no_go int OUTPUT AS SET NOCOUNT ON SELECT user_id FROM profile WHERE user_id=\@id AND pswd=\@pswd IF \@\@ROWCOUNT = 0 BEGIN
2
2007
by: james cox via SQLMonster.com | last post by:
I don't know if this is possible. However, what i am attempting to do is using C#'s window forms. I open up an excell sheet stored in my windows form. The excel sheet stores names of the stored procedures in that database. I want to know if it's possible to click on that stored procedure to open up a link to display the code of that stored procedure of course in a read only mode. any suggestions....
6
2361
by: SandySears | last post by:
I am trying to use a stored procedure to insert a record using VS 2005, VB and SQL Server Express. The code runs without errors or exceptions, and returns the new identifer in the output parameters. It returns my error text message in another output parameter as "ok", which is the value that is set in the stored procedure prior to doing the insert. It returns my var for @@rowcount as 1. However, the record does not get into the table. ...
7
9716
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason for this being to stop the user thinking the application has frozen when in fact it is just waiting for a long SP to complete. Another reason for doing it like this is that I also have had a problem in the past where the SP takes longer than the...
3
3999
by: mch1ck3y | last post by:
Hello there - hoping someone can offer some advice. I am using SQL Server 2000 and want a stored procedure to execute 20 seconds after a particular field on a table is updated using a trigger on the table to be updated. Any ideas on how this would be achievable - any help would be greatly appreciated.
4
5077
by: barmatt80 | last post by:
I am stumped on the error reporting with sql server. I was told i need to return @SQLCode(code showing if successful or not) and @ErrMsg(and the message returned). I am clueless on this. I wrote this procedure: ALTER PROCEDURE . @Emp_SSN int, @Annual_Forward decimal(10,2),
3
2545
by: stockton | last post by:
I have written the following Stored Procedure in an attempt to update two tables in the same database reliably but unfortunately it is not too successful. I ocassionaly end up with only the BundlesIssued tables updated and nothing in TicketsIssued. Please make suggestions on how I could make this stored procedure update both tables reliably. ALTER PROCEDURE spIssueScannedTickets @iEventID int, @MemberNum nvarchar(12), ...
2
2326
by: Jeganath | last post by:
Hi, I have written a Stored Procedure. I'm getting the error as below. Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32). Can any one give me a solution for this problem I have given the procedure below. ALTER procedure .
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.