Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 26th, 2005, 05:29 AM
Authorized User
 
Join Date: Jan 2005
Location: , , .
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP and auto-generated primary keys

Hi,

I have an ASP application that accesses an SQL Server 2000 database.

On one of my ASP pages, I want to create a NEW record in a table, and IMMEDIATELY (i.e. in later code on that same page) have access to the primary key of that record.

If I use a primary key that is an auto-generated number (in the database) it seems that my ASP cannot immediately know what that number is. I am therefore forced to use non-auto-generated primary keys in the table, and generate the primary key of the new record manually in the ASP, so that I immediately know what it is.

Is this correct, or is there a better way? Am I missing something? I would prefer it if I can always use auto-generated primary keys in my tables.

Hope you can help.

Thanks.

Steve

P.S. The auto-generated primary keys that I am taking about are sequential integers.
 
Old May 26th, 2005, 06:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

best practice is to use a stored procedure to create the record and return the value of @@IDENTITY as an output parameter

hth
Phil
 
Old May 26th, 2005, 10:47 AM
Friend of Wrox
Points: 410, Level: 7
Points: 410, Level: 7 Points: 410, Level: 7 Points: 410, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: New Delhi, India.
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anubhav.kumar
Default

hi,

Another alternative is to use the serverside cursor instead a client side
Write something like this before opening the connection

MyCon.CursorLocation=aduseServer
Mycon.open "Provi....... etc.

After that open a dynamic cursor based recordset for adding a new row
Add values in the field and call update method. Just after update you can access the value of autogenerated value from the field without closing the recordset.

cheers

Anubhav Kumar




Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting records with foreign and primary keys tdaustin Classic ASP Basics 0 October 4th, 2005 12:49 AM
When to define primary keys and foregin keys? method SQL Server 2000 1 August 26th, 2005 09:14 AM
Find() With multiple primary keys busher ASP.NET 1.0 and 1.1 Professional 0 June 2nd, 2005 03:00 PM
Thoughts on Primary keys rohan_man Access 3 February 9th, 2005 06:18 PM
Subform/Links ~ Too Many Primary Keys fdsi19 Access 1 September 30th, 2003 12:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.