473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 23458
The SqlCommand.ExecuteNonQuery will return the rowcount for non queries. In
SELECT statements, well, just count how many rows you get :).
-mike
MVP

"Richard G" <ri*********@gte.net> wrote in message
news:%2****************@TK2MSFTNGP11.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.ExecuteNonQuery();

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*******@Atrevido.net> wrote in message
news:eQ**************@TK2MSFTNGP12.phx.gbl...
The SqlCommand.ExecuteNonQuery will return the rowcount for non queries. In SELECT statements, well, just count how many rows you get :).
-mike
MVP

"Richard G" <ri*********@gte.net> wrote in message
news:%2****************@TK2MSFTNGP11.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****@speakeasy.net> wrote in message
news:uf********************@speakeasy.net...
Mike-

int count = command.ExecuteNonQuery();

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*******@Atrevido.net> wrote in message
news:eQ**************@TK2MSFTNGP12.phx.gbl...
The SqlCommand.ExecuteNonQuery will return the rowcount for non queries.

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

"Richard G" <ri*********@gte.net> wrote in message
news:%2****************@TK2MSFTNGP11.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\arizona\residentialExpo rt.txt' WITH
(FIELDTERMINATOR = '|', ROWTERMINATOR='\n')

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

{

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

connection = new SqlConnection(connectionString);

SqlCommand command = new SqlCommand( query, connection );

try

{

connection.Open();

command.CommandTimeout = 6000;

int count = command.ExecuteNonQuery();

Console.WriteLine( "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.State == ConnectionState.Open )

connection.Close();

connection.Dispose();

if( command != null )

command.Dispose();

}

}
"Michael Giagnocavo [MVP]" <mg*******@Atrevido.net> wrote in message
news:%2****************@TK2MSFTNGP11.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****@speakeasy.net> wrote in message
news:uf********************@speakeasy.net...
Mike-

int count = command.ExecuteNonQuery();

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*******@Atrevido.net> wrote in message
news:eQ**************@TK2MSFTNGP12.phx.gbl...
The SqlCommand.ExecuteNonQuery will return the rowcount for non
queries. In
SELECT statements, well, just count how many rows you get :).
-mike
MVP

"Richard G" <ri*********@gte.net> wrote in message
news:%2****************@TK2MSFTNGP11.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
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...
3
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...
2
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...
6
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...
7
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...
3
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...
4
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...
3
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.