Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-09-01 Thread Lennin Caro
--- On Thu, 8/28/08, Masis, Alexander (US SSA) [EMAIL PROTECTED] wrote: From: Masis, Alexander (US SSA) [EMAIL PROTECTED] Subject: [GENERAL] MySQL LAST_INSERT_ID() to Postgres To: pgsql-general@postgresql.org Date: Thursday, August 28, 2008, 4:14 PM I was mapping C++ application code

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-29 Thread Magnus Hagander
Alvaro Herrera wrote: Russ Brown escribió: Masis, Alexander (US SSA) wrote: SELECT CURRVAL( pg_get_serial_sequence('my_tbl_name','id_col_name')); Any reason why you can't just do this? CREATE FUNCTION last_insert_id() RETURNS bigint AS $$ SELECT lastval(); $$ LANGUAGE SQL VOLATILE;

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-29 Thread Alvaro Herrera
Magnus Hagander escribió: Alvaro Herrera wrote: Russ Brown escribió: Masis, Alexander (US SSA) wrote: SELECT CURRVAL( pg_get_serial_sequence('my_tbl_name','id_col_name')); Any reason why you can't just do this? CREATE FUNCTION last_insert_id() RETURNS bigint AS $$ SELECT

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-29 Thread Raymond O'Donnell
On 29/08/2008 05:45, Tom Lane wrote: A general comment on those pages is that the tabular lists of functions are intended to give one-liner descriptions of what the functions do. For cases where a one-liner isn't sufficient, there's a sentence or a paragraph following the table. I don't

[GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Masis, Alexander (US SSA)
I was mapping C++ application code that works with mySQL to work with Postgres. There were a number of articles on line regarding the conversion from mySQL to Postgres like: http://en.wikibooks.org/wiki/Programming:Converting_MySQL_to_PostgreSQL http://groups.drupal.org/node/4680

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Scott Marlowe
On Thu, Aug 28, 2008 at 10:14 AM, Masis, Alexander (US SSA) [EMAIL PROTECTED] wrote: I was mapping C++ application code that works with mySQL to work with Postgres. There were a number of articles on line regarding the conversion from mySQL to Postgres like: SNIP Well, in MySQL it's easy you

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Russ Brown
Masis, Alexander (US SSA) wrote: SELECT CURRVAL( pg_get_serial_sequence('my_tbl_name','id_col_name')); Any reason why you can't just do this? CREATE FUNCTION last_insert_id() RETURNS bigint AS $$ SELECT lastval(); $$ LANGUAGE SQL VOLATILE; -- Sent via pgsql-general mailing list

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Steve Atkins
On Aug 28, 2008, at 12:09 PM, Scott Marlowe wrote: On Thu, Aug 28, 2008 at 10:14 AM, Masis, Alexander (US SSA) [EMAIL PROTECTED] wrote: I was mapping C++ application code that works with mySQL to work with Postgres. There were a number of articles on line regarding the conversion from mySQL

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Scott Marlowe
On Thu, Aug 28, 2008 at 1:56 PM, Steve Atkins [EMAIL PROTECTED] wrote: Or lastval() if you want something bug-compatible with MySQL. Not exactly. LAST_INSERT_ID is transactionally safe in that one connection doesn't see another connections. However, it has it's own special brand of bug that

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Bill
Masis, Alexander (US SSA) wrote: I was mapping C++ application code that works with mySQL to work with Postgres. There were a number of articles on line regarding the conversion from mySQL to Postgres like: http://en.wikibooks.org/wiki/Programming:Converting_MySQL_to_PostgreSQL

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Raymond O'Donnell
On 28/08/2008 22:26, Bill wrote: someone confirm that currval() returns the the value for the connection from which it is called? Yes, see here: http://www.postgresql.org/docs/8.3/static/functions-sequence.html and specifically a little further down the page on currval: Return the value

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Bill
Steve Atkins wrote: On Aug 28, 2008, at 12:09 PM, Scott Marlowe wrote: On Thu, Aug 28, 2008 at 10:14 AM, Masis, Alexander (US SSA) [EMAIL PROTECTED] wrote: I was mapping C++ application code that works with mySQL to work with Postgres. There were a number of articles on line regarding the

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Scott Marlowe
On Thu, Aug 28, 2008 at 3:38 PM, Bill [EMAIL PROTECTED] wrote: I am new to PostgreSQL but it seems to me that lastval() will only work if the insert does not produce side effects that call nextval(). Consider the case where a row is inserted into a table that has an after insert trigger and

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Joshua Drake
On Thu, 28 Aug 2008 16:06:14 -0600 Scott Marlowe [EMAIL PROTECTED] wrote: On Thu, Aug 28, 2008 at 3:38 PM, Bill [EMAIL PROTECTED] wrote: I am new to PostgreSQL but it seems to me that lastval() will only work if the insert does not produce side effects that call nextval(). Consider the

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Alvaro Herrera
Russ Brown escribió: Masis, Alexander (US SSA) wrote: SELECT CURRVAL( pg_get_serial_sequence('my_tbl_name','id_col_name')); Any reason why you can't just do this? CREATE FUNCTION last_insert_id() RETURNS bigint AS $$ SELECT lastval(); $$ LANGUAGE SQL VOLATILE; If your table

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread D. Dante Lorenso
Scott Marlowe wrote: On Thu, Aug 28, 2008 at 3:38 PM, Bill [EMAIL PROTECTED] wrote: I am new to PostgreSQL but it seems to me that lastval() will only work if the insert does not produce side effects that call nextval(). Consider the case where a row is inserted into a table that has an after

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Christophe
On Aug 28, 2008, at 3:23 PM, D. Dante Lorenso wrote: I use RETURNING for all my insert and UPDATE statements now. Usually I'll return the primary key for the table, but sometimes I return a column that is created by one of my triggers. It's awesome to be able to do this in one query.

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Bill
Scott Marlowe wrote: On Thu, Aug 28, 2008 at 3:38 PM, Bill [EMAIL PROTECTED] wrote: I am new to PostgreSQL but it seems to me that lastval() will only work if the insert does not produce side effects that call nextval(). Consider the case where a row is inserted into a table that has an

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Joshua Drake
On Thu, 28 Aug 2008 15:29:51 -0700 Bill [EMAIL PROTECTED] wrote: The PostgresSQL 8.3 help file clearly shows that lastval() does not take a sequence as a parameter and the description i is Return the value most recently returned by |nextval| in the current session. This function is identical

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Scott Marlowe
On Thu, Aug 28, 2008 at 4:18 PM, Joshua Drake [EMAIL PROTECTED] wrote: On Thu, 28 Aug 2008 16:06:14 -0600 Scott Marlowe [EMAIL PROTECTED] wrote: No, setval, currval, and lastval all require as an argument a sequence name. So the real issue is you have to know the sequence name to use them.

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Joshua Drake
On Thu, 28 Aug 2008 16:46:19 -0600 Scott Marlowe [EMAIL PROTECTED] wrote: On Thu, Aug 28, 2008 at 4:18 PM, Joshua Drake [EMAIL PROTECTED] wrote: On Thu, 28 Aug 2008 16:06:14 -0600 Scott Marlowe [EMAIL PROTECTED] wrote: No, setval, currval, and lastval all require as an argument a

Re: [GENERAL] MySQL LAST_INSERT_ID() to Postgres

2008-08-28 Thread Tom Lane
Raymond O'Donnell [EMAIL PROTECTED] writes: On 28/08/2008 22:26, Bill wrote: someone confirm that currval() returns the the value for the connection from which it is called? Yes, see here: http://www.postgresql.org/docs/8.3/static/functions-sequence.html and specifically a little further