Solved

Connection objects and last_insert_id() help !

Posted on 2004-04-17
3
409 Views
Last Modified: 2012-05-04
Hey everyone,

I'm ripping my hair out over this problem:

I have a Insert statement, and i want to retrieve the last_insert_ID() after it has executed so it goes like this:

SQLINS = "INSERT INTO family_albums (category,fam_ID,album_name,hidden,system_album,in_progress,user_ID) values(null,'"&request.cookies("access")("family_ID")&"','"&Upload.Form("Albumselect")&Upload.Form("Albumcreate")&"',1,0,1,"&request.cookies("access")("user_ID")&")"
objCmd.CommandText = sqlins
objCmd.Execute
RS1.open "SELECT LAST_INSERT_ID() AS `album_ID`" , "DSN=xxxx; uid=xxxx;pwd=xxxx"
session("varalbum_ID") = RS1("album_ID")
RS1.close

That returns a RS1("album_ID") of 0.

HOWEVER, when i do
SQLINS = "INSERT INTO family_photos (fam_ID,addedby_user,album_ID,file_name) values("&request.cookies("access")("family_ID")&","&request.cookies("access")("user_ID")&",LAST_INSERT_ID(),'"&file.ExtractfileName&"')"
objCmd.CommandText = sqlins
objCmd.Execute

the last_insert_ID() works (HOWEVER, I can't use it because it is in a loop and would increment itself (when i dont want it to)

I think i've answered my own question.. but i still dont know how to make it work.
I'm asuming the problem is that i'm opening a whole new connection object and it's not finding any last inserted id's.

So my question if the above is right, how would i do a "SELECT" on a objcmd and return a value in a RS("") fashion ?

Thanks for any insight!\

Chris
0
Comment
Question by:garfield1979
3 Comments
 

Accepted Solution

by:
jameswalt earned 200 total points
ID: 10850723
I've had this problem many times. Since an insert does not have any return values, we could always execute a query such as follows:
This is the main problem with using autoincrements as primary keys (that and the additional storage required)

CONNECT.EXECUTE INSERT STATEMENT
RS1.OPEN "SELECT Max(album_ID) AS lastID FROM family_albums"

Response.write RS1("lastID")


I hope this helps - it's pretty much what you supposed, but a more clear solution.
0
 

Author Comment

by:garfield1979
ID: 10850752
Thanks James.

That solution wil work, i'm at nerves end and i'm about to throw this system against the wall..

:-D

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10851342
The main problem with this approach is performance and in a multi-user environment you could pick up the wrong value.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

805 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