Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. 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 Databases 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 July 15th, 2004, 06:52 AM
Registered User
 
Join Date: Jul 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default get last record id using mySQL

Hi all,

I'm using MySQL with ASP and am trying to retrieve a new record ID immediately after insertion so I can work with it uniquely.

No matter what I do, I always return a blank when trying to use either @@IDENTITY or LAST_INSERT_ID().

Could someone please advise the correct query structure I should be using?

This is my connection method which works OK and correctly inserts a new record.

Dim RS
set RS= Server.CreateObject("ADODB.Command")
RS.ActiveConnection = "Driver={MySQL}; DATABASE=myData; UID=myUser; PASSWORD=myPassword;"
RS.CommandText = "INSERT INTO table_name (field_name) VALUES ('field_value')"
RS.Execute

all advice gratefully received :)

thanks

Greg
 
Old July 15th, 2004, 08:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Greg,

Check out this page.

How to Get the Unique ID for the Last Inserted Row

IMO, you should do that all in one transaction. May be write a procedure in mysql that takes all the parameters for Insert and returns the Unique Id. When you do that with 2 recordsets as 2 transactions, you won't be getting the value at all, as it(insert and getting Unique ID) has to be done as a single transaction, which I think you are missing there.

Or construct the SQL string as given below

Code:
strsql = "insert into TABLE_NAME (COL1, Col2) values('You','Me');
strsql = strsql & "select last_insert_id() as NewUniqueId;"
RS.CommandText = strsql
RS.Execute
Response.write RS("NewUniqueId");
Something like this should help you getting that out.


Else you will have to write a procedure that inserts values into table and returns the UniqueId back.

--Create you procedure in MYSQL
Code:
CREATE PROCEDURE InsertVal (IN param1 INT, IN param2 VARCHAR(30))
BEGIN
    Insert into TABLENAME(Col1, Col2) Values(param1,param2);
    SELECT last_insert_id() as NewUniqueId;
END
'In ASP, your code should be like
Code:
strsql = "Call InsertVal(10,'yourname');"
RS.CommandText = strsql
RS.Execute
Response.write RS("NewUniqueId");
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old July 15th, 2004, 09:38 AM
Registered User
 
Join Date: Jul 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi happygv, thanks for the quick response.

Using your first suggestion, I get an error when trying to concat both 'strsql' strings together.

I'm using mySQL 4.0 (win32) locally and mySQL 3.23.56 (linux/chilisoft) on the web server.

I get errors at the same place on both, at the ';' separator in the CommandText line.

Are these versions compatible with this method?

thanks

Greg



 
Old July 15th, 2004, 10:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Just noticed, there was a " missing in my previous post.

strsql = "insert into TABLE_NAME (COL1, Col2) values('You','Me');"

If you have corrected that at your end and still you get that error, probably you can try removing those ;s from there and see if that works.

Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old July 15th, 2004, 12:46 PM
Registered User
 
Join Date: Jul 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I did spot the missing " but it makes no difference.

It just doesn't seem to like the INSERT... SELECT combination.

I had a look here

http://dev.mysql.com/doc/mysql/en/INSERT_SELECT.html

and tried various key options but I'm still rather stuck, whatever I try just returns 0 or blank or an error message.

Ho hum

Greg
 
Old July 17th, 2004, 11:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

That INSERT... SELECT combination is to populate the table with values from another table. That is not the one going to help you in this. So you don't have to worry about that link you posted.

Have you tried the second option, doing it with the help of creating stored procedure?

Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old May 11th, 2008, 08:29 PM
Registered User
 
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Does anyone have any feedback, keep getting 'type mismatch' error...Trying to get the auto-increment value. Any help would be great!!
Code:
SQL_RunNum="SELECT LAST_INSERT_ID()"
dbconn.execute(SQL_RunNum)
response.write(SQL_RunNum(0))





Similar Threads
Thread Thread Starter Forum Replies Last Post
Last record inserted ID ADAC Programming VB Databases Basics 1 June 5th, 2006 02:41 PM
How to open only the record with highest ID ?! SKE Classic ASP Databases 2 May 16th, 2005 06:05 AM
Capture USER ID in record fixitman Access 3 February 9th, 2005 03:56 PM
Record ID sinner Classic ASP Databases 12 February 28th, 2004 03:36 PM
Insert new record ID levinho Classic ASP Databases 5 January 14th, 2004 12:03 PM





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