Groups | Blog | Home
all groups > sql server programming > june 2006 >

sql server programming : SET NOCOUNT ON?


Mike C#
6/17/2006 8:43:36 PM
As I recall there is an issue with ASP (not with ASP.NET) where SQL Server
informational messages can cause problems retrieving actual data. SET
NOCOUNT ON before the statement is the fix for that. Also as
newscorrespondent wrote, use SCOPE_IDENTITY() instead of @@IDENTITY.

If you need more help, post your DDL, DML, yadda yadda.

Adam Machanic
6/17/2006 10:33:40 PM
To add to what Mike C# said, I'm betting you'll find your expected result if
you use NextRecordset() (? -- been a while since I've done ADO work, but
it's something like that). The row count (which is turned off by SET
NOCOUNT ON) comes back as a result set in ADO. Luckily, that is fixed in
ADO.NET.


--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--


[quoted text, click to view]

Rob Meade
6/17/2006 11:11:14 PM
Hi all,

I'm trying to insert a record into a database via ASP, and then extract the
id of the current row using @@IDENTITY...

I've read the articles on ASPAQ and somewhere else regarding the use of the
above, but I do actually want the ID of the row inserted using this
connection, not just the most recent record if that makes any sense...

Now - what's frustrating me the most at the moment, is that when I execute
my stored procedure via my code ( the same code I use else where with no
problems ) - I do not get a value back for the @@IDENTITY in ASP (I do in
SQL if I run the SQL statement that I generated)..

The only way I seem to be able to get a response is to add "SET NOCOUNT ON;
" infront of my SQL statement - doing so gets me a value back - Hooray....

However, that cheer is short lived because for some bizarre reason - I now
get 4 rows inserted into the database each time i run it!?!

There is NO loop anyway near my inserting code..and if I take the SET
NOCOUNT ON; out of the statement it does infact run correctly and insert
just the one row and I'd expect but - no returned value...

Has anyone else come across this bizarre behaviour before?

I use this same process else where and my technique (good or bad) is the
same, I dont understand why its going wrong?!

Any help would be appreciated....

Regards

Rob

Rob Meade
6/18/2006 12:00:00 AM
Hi all,

I'd already put the SET NOCOUNT ON at the top of the stored procedure, and
to ensure, I had placed in front of my SQL statment, separated by a
sermi-colon.

I ran a trace in SQL Profiler and the statement was being executed 4 times!

I found that by not using a DSN-less connection in my ASP page, and instead
using a DSN the problem went away - now - to me that just seems like
crazy...

Any thoughts?

Regards

Rob

Rob Meade
6/18/2006 12:00:00 AM
Oh, and I had tried the SCOPE_IDENTITY() as well, from reading though I'm
not sure that that was what I wanted..

I wanted to get the "just inserted" row by my code at that time, not the
most recently inserted id in the table...think there was another option I
could have used as well...

What was most strange about this was my method is being used else where in
another part of my application - the only difference was the DSN-less/DSN
connection - I still dont undestand why this would execute 4 times thought?

Rob

newscorrespondent NO[at]SPAM charter.net
6/18/2006 12:31:50 AM


Take a look in BOL at SCOPE_IDENTITY() and use that instead of @@IDENTITY

Dan Guzman
6/18/2006 8:13:19 AM
[quoted text, click to view]

SCOPE_IDENTITY() will do what you want; it will return the last IDENTITY
value inserted on the current database connection. The difference between
@@IDENTITY and SCOPE_IDENTITY() is that SCOPE_IDENTITY() will return the
last value within the current SCOPE and is therefore not affected by
triggers.

[quoted text, click to view]

If the entire procedure is being executed multiple times, then it is likely
that you have an issue in your application code. It's interesting that the
problem doesn't surface until you add SET NOCOUNT ON. Have you tried adding
SET NOCOUNT ON at the *beginning* of the proc (immediately after 'AS')?

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Aaron Bertrand [SQL Server MVP]
6/18/2006 9:04:56 AM
[quoted text, click to view]


Could you show your code???

Mike C#
6/18/2006 11:04:36 AM
Is the statement being "executed" 4 times, or is it merely showing up 4
times because you've selected several events that report the statement's
progress?

[quoted text, click to view]

Mike C#
6/18/2006 11:07:16 AM

[quoted text, click to view]

If you want the actual "just inserted row", you can use a trigger that
SELECTs the row from the INSERTED virtual table, or you can do a SELECT ...
FROM mytable WHERE [Id] = SCOPE_IDENTITY(). If you want just the identity
number inserted, SCOPE_IDENTITY() will do the trick.

Adam Machanic
6/18/2006 12:10:12 PM
[quoted text, click to view]

I seem to remember some ADO issue where a statement would be executed
twice if you had the wrong settings turned on -- once so ADO could bind to
the output columns, and then again to get the actual data... Unfortunately,
I can't recall what the option was (and again, this would be only twice, not
four times)... Working with ADO.NET is so much nicer :-)


--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--

Mike C#
6/18/2006 9:12:13 PM

[quoted text, click to view]

With ODBC, would that be the SQLPrepare function reporting back as a second
execution? Been a while since I messed with the ODBC SDK, but hey... Who
knows, might be that he just needs to update to a newer version of MDAC...

AddThis Social Bookmark Button