Solved

Retrieve Scope_identity() in ASP

Posted on 2007-04-03
5
992 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

743 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

10 Experts available now in Live!

Get 1:1 Help Now