I know that, if I execute a single SQL statement that UPDATEs or DELETEs some data, that it will return the number of rows affected.
But if I have multiple SQL statements in a sql script, and I want to know the number of rows affected from the last statement executed, will it still return that automatically, or do I need a
SELECT @@RowCount
at the end of the script?
The code in question is not a Stored Procedure. Rather, it is a parameterized SQL script stored in an arbitrary location, executed using the ExecuteStoreCommand
function in Entity Framework, as in:
var numberOfRowsAffected = context.ExecuteStoreCommand<int>(mySqlScript, parameters);
It depends on the NOCOUNT
setting when executing your quer(y/ies).
If NOCOUNT
is ON
then no DONE_IN_PROC
messages will NOT be returned.
If NOCOUNT
is OFF
, the default setting, then DONE_IN_PROC
messages will be returned, (eg. counts).
Both of these situations are different to executing,
SELECT @@ROWCOUNT;
which will return a result set with a single scalar value, different from a DONE_IN_PROC message. This will occur, regardless of the setting of NOCOUNT
.
I believe that SELECT @@ROWCOUNT
is sometimes used to make Entity Framework "play" with more complex TSQL statements because EF both requires
DONE_IN_PROC
message. Its important that SELECT @@ROWCOUNT;
is executed immediately after the last query statement because many statements will reset @@ROWCOUNT
and therefore yield an unexpected result.
Just to be specific on answer part, you would need to add SELECT @@RowCount to return number of rows affected by last statement.
I think confusion might be due to rows returned in SSMS window while executing query.By default SSMS shows number of rows returned for all sql statements but it returns affected rows as message not a dataset .
@@ROWCOUNT will automatically return number of rows effected by the last statement.
Please find the msdn link here https://msdn.microsoft.com/en-us/library/ms187316.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.