cannot get a value from scope_identity after insert

Posted on 2008-11-10
Last Modified: 2012-05-05
In ASp i am doing this:

set objrs=objconn.execute("insert into cart (title) values ('something');select SCOPE_IDENTITY() as id;")

When I check objrs("ID") it returns blank.  Nothing.  This works fine access but not in SQL 2000.  I find that it seems to have something to do with it being in the second line of the SQL.  if I replace the scope_identity() with max(cartid) i get the same thing.  So the second line is not returning a value.

Running this in the query analyzer DOES return the value as expected.

Any ideas?
Question by:jglazer63
    LVL 8

    Expert Comment

    Have you tried running this query as a stored procedure?  I seem to remember running into this a while back and setting up an SP fixed it.

    Author Comment

    I do not want to do this as a stored procedure.  I want to do this entirely with the ADO execute.  It needs to be very generic.
    LVL 39

    Accepted Solution

    Or perhaps the "1 rows effected" by the insert (or subsequent triggers) is messing with it.

    set objrs=objconn.execute("set nocount on; insert into cart (title) values ('something');select SCOPE_IDENTITY() as id;")
    LVL 39

    Expert Comment

    Also, although I know you say you don't want a proc, but a proc is the BEST thing for DB processing.
    LVL 8

    Expert Comment

    That's it -- SET NOCOUNT ON...I knew there was something I was missing.  Brandon's right on the money.

    Author Closing Comment

    Perfect.  You got it.  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now