Solved

Retrieve Scope_identity() in ASP

Posted on 2007-04-03
5
996 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
  • 3
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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 142

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 142

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Viewers will learn how the fundamental information of how to create a table.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

910 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

22 Experts available now in Live!

Get 1:1 Help Now