By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

Multiple Statements in a query

P: n/a
I have 2 statements that I'd like to string together in a query, but
according to everything I'm seeing, it's not possible.
What I'm doing is an insert of a row, where the primary key is an auto
increment. I want to retrieve that by using LAST_INSERT_ID(). I
tried sending another query after the first (once I knew that it the
data was what I need) but it didn't return anything. It seems that
that function can't be used in separate calls or something. Either
that or there's a setting that I need to check. Any ideas?

Mar 2 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Fri, 02 Mar 2007 11:28:02 -0800, pa***********@gmail.com wrote:
I have 2 statements that I'd like to string together in a query, but
according to everything I'm seeing, it's not possible.
What I'm doing is an insert of a row, where the primary key is an auto
increment. I want to retrieve that by using LAST_INSERT_ID(). I
tried sending another query after the first (once I knew that it the
data was what I need) but it didn't return anything. It seems that
that function can't be used in separate calls or something. Either
that or there's a setting that I need to check. Any ideas?
If you're trying to return the last inserted row on a table with an auto
increment field all you have to do is query the maximum value in that
field.

Mar 2 '07 #2

P: n/a
..oO(Ivan Marsh)
>On Fri, 02 Mar 2007 11:28:02 -0800, pa***********@gmail.com wrote:
>I have 2 statements that I'd like to string together in a query, but
according to everything I'm seeing, it's not possible.
What I'm doing is an insert of a row, where the primary key is an auto
increment. I want to retrieve that by using LAST_INSERT_ID(). I
tried sending another query after the first (once I knew that it the
data was what I need) but it didn't return anything. It seems that
that function can't be used in separate calls or something. Either
that or there's a setting that I need to check. Any ideas?
Neither nor, there's an error in your second query. Please post some
code and the used query.
>If you're trying to return the last inserted row on a table with an auto
increment field all you have to do is query the maximum value in that
field.
Wrong! Never do it that way, never! Google for "race condition" and you
will know why. The correct way to do it is to call LAST_INSERT_ID(),
either natively in a query or through a higher API function like
mysql_insert_id().

Micha
Mar 3 '07 #3

P: n/a
Ivan Marsh wrote:
On Fri, 02 Mar 2007 11:28:02 -0800, pa***********@gmail.com wrote:
>I have 2 statements that I'd like to string together in a query, but
according to everything I'm seeing, it's not possible.
What I'm doing is an insert of a row, where the primary key is an auto
increment. I want to retrieve that by using LAST_INSERT_ID(). I
tried sending another query after the first (once I knew that it the
data was what I need) but it didn't return anything. It seems that
that function can't be used in separate calls or something. Either
that or there's a setting that I need to check. Any ideas?

If you're trying to return the last inserted row on a table with an auto
increment field all you have to do is query the maximum value in that
field.
And if you have two people insert at the same time one is going to get
the wrong value.

A terrible way to do it!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '07 #4

P: n/a
I have 2 statements that I'd like to string together in a query, but
according to everything I'm seeing, it's not possible.
What I'm doing is an insert of a row, where the primary key is an auto
increment. I want to retrieve that by using LAST_INSERT_ID(). I
tried sending another query after the first (once I knew that it the
data was what I need) but it didn't return anything. It seems that
that function can't be used in separate calls or something. Either
that or there's a setting that I need to check. Any ideas?
It is already suggested that you post the queries themselves. But it
could be something else: If you issue the queries in separate
connections, they will not work together. A connection in MySQL (I
assume you are using MySQL) is like a session: variables only exist
within that session and the result of LAST_INSERT_ID is also not visible
to other connections.

So you do issue the queries in one connection, I hope?

Best regards
Mar 3 '07 #5

P: n/a
Jerry Stuckle wrote:
And if you have two people insert at the same time one is going to get
the wrong value.

A terrible way to do it!
That has changed now, hasn't it?
My understanding now is that it will return the last value
created *on that particular connection*.

p.s. I'm still working on that join tutorial you gave me. Thx.
Mar 3 '07 #6

P: n/a
Sanders Kaufman wrote:
Jerry Stuckle wrote:
>And if you have two people insert at the same time one is going to get
the wrong value.

A terrible way to do it!

That has changed now, hasn't it?
My understanding now is that it will return the last value created *on
that particular connection*.

p.s. I'm still working on that join tutorial you gave me. Thx.
No,

SELECT MAX(id) FROM mytable;

always returns the maximum value, no matter who inserted it.
You're thinking about mysql_last_insert_id(), which is connection specific.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '07 #7

P: n/a
On Mar 3, 1:07 pm, Dikkie Dik <nos...@nospam.orgwrote:
I have 2 statements that I'd like to string together in a query, but
according to everything I'm seeing, it's not possible.
What I'm doing is an insert of a row, where the primary key is an auto
increment. I want to retrieve that by using LAST_INSERT_ID(). I
tried sending another query after the first (once I knew that it the
data was what I need) but it didn't return anything. It seems that
that function can't be used in separate calls or something. Either
that or there's a setting that I need to check. Any ideas?

It is already suggested that you post the queries themselves. But it
could be something else: If you issue the queries in separate
connections, they will not work together. A connection in MySQL (I
assume you are using MySQL) is like a session: variables only exist
within that session and the result of LAST_INSERT_ID is also not visible
to other connections.

So you do issue the queries in one connection, I hope?

Best regards
I actually solved it a different way. I was using a unique value to
create the table as it was, so I just used that instead.

I was using "SELECT LAST_INSERT_ID();" as my second query. The first
just being a general insert. I'm positive there was nothing wrong
with my queries because I put them in the console, and it worked
fine. I think there might be some issues with my db wrapper, but
that's a separate issue I think.

Mar 4 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.