Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieve Scope_identity() in ASP

Posted on 2007-04-03
5
Medium Priority
?
1,014 Views
Last Modified: 2012-06-21
I have an MS Sql Table 'order_number' withFields: 'order_id scope_identity()',  'pplno'

Using a transaction i want to retrieve and store the identity value as a variable when i insert new a record in pplno using ASP. Can anyone help? Below is what i have so far.

Many Thanks,
SM

StrConnect_ppl = "DRIVER=SQL Server;" & _
      "SERVER=;UID=;PWD=DATABASE=;"

      On Error Resume Next
      'CREATE UNIQUE ORDER NUMBER
      SQLStmt_1 = "Insert into order_number(ppl_no) values ('"& pplno &"') Select scope_identity()"

                    Set Cn = Server.CreateObject("ADODB.Connection")
                Cn.Open StrConnect_ppl
              Cn.BeginTrans
 
      Set Rs = Server.CreateObject("ADODB.Recordset")
               Rs.Open SQLStmt_1,Cn,1,3
       cn.CommitTrans
   cn.Close      
      order_id = Rs(0)
0
Comment
Question by:seanmorris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 18843406
StrConnect_ppl = "DRIVER=SQL Server;" & _
      "SERVER=;UID=;PWD=DATABASE=;"

      On Error Resume Next
      'CREATE UNIQUE ORDER NUMBER
      SQLStmt_1 = " SET NOCOUNT ON  Insert into order_number(ppl_no) values ('"& pplno &"') Select scope_identity() new_id_value "

                    Set Cn = Server.CreateObject("ADODB.Connection")
                Cn.Open StrConnect_ppl
              Cn.BeginTrans
 
      Set Rs = Server.CreateObject("ADODB.Recordset")
               Rs.Open SQLStmt_1,Cn,1,3
       cn.CommitTrans
   cn.Close      
      order_id = Rs("new_id_value").value


0
 

Author Comment

by:seanmorris
ID: 18843594
hi angellll

Its still not returning the new_id_value to order_id..

Sean
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18843641
do you get any errors?
does the table order_number have a identity field?
can you run the SQL manually against the database (query analyser), does it run fine there?
0
 

Author Comment

by:seanmorris
ID: 18843705
Hi angelll

Yes it works manually through query analyser. I've modified the code below and it now works. Must have been because i was trying to retrieve scope_identity() after the transaction was complete. Does this seem logical to you?

Set Rs = Server.CreateObject("ADODB.Recordset")
               Rs.Open SQLStmt_1,Cn,1,3
               order_id = Rs("new_id_value").value
       cn.CommitTrans
   cn.Close      
     
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18843726
I rather think it was because you tried to access it after the cn.close
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

730 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