Solved

Retrieve Scope_identity() in ASP

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

756 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