Solved

Retrieve Scope_identity() in ASP

Posted on 2007-04-03
5
1,004 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 143

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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please explain purpose of GZIP 4 34
SQL Query 2 31
Help With SQL Query 9 30
use of sqlCmd command through exec master..xp_cmdshell 7 14
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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