PostgreSQL and select nextval

This is a discussion on PostgreSQL and select nextval within the PHP General forums, part of the PHP Programming Forums category; Hi, I'm getting an error message when i run the following SQL request : $sql = "INSERT INTO tmp_importedxls (rec_id, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-12-2007
Alain Roger
 
Posts: n/a
Default PostgreSQL and select nextval

Hi,

I'm getting an error message when i run the following SQL request :
$sql = "INSERT INTO tmp_importedxls (rec_id, publisher) VALUES (SELECT
nextval('tmp_importedxls_rec_id_seq'),'$pb')";

Error in SQL query: ERROR: syntax error at or near "SELECT" LINE 2: VALUES
(SELECT nextval('tmp_importedxls_rec_id_seq'),' ^

I have the feeling that we can not use the select nextval(...) SQL request
in an INSERT INTO one under PHP.
Is it true?

thanks
--
Alain
------------------------------------
Windows XP SP2
PostgreSQL 8.2.3
Apache 2.2.4
PHP 5.2.3

Reply With Quote
  #2 (permalink)  
Old 08-12-2007
brian
 
Posts: n/a
Default Re: [PHP] PostgreSQL and select nextval

Alain Roger wrote:
> Hi,
>
> I'm getting an error message when i run the following SQL request :
> $sql = "INSERT INTO tmp_importedxls (rec_id, publisher) VALUES (SELECT
> nextval('tmp_importedxls_rec_id_seq'),'$pb')";
>
> Error in SQL query: ERROR: syntax error at or near "SELECT" LINE 2: VALUES
> (SELECT nextval('tmp_importedxls_rec_id_seq'),' ^
>
> I have the feeling that we can not use the select nextval(...) SQL request
> in an INSERT INTO one under PHP.
> Is it true?
>


If rec_id is a SERIAL it will increment itself:

INSERT INTO tmp_importedxls (publisher) VALUES ('$pb');

brian
Reply With Quote
  #3 (permalink)  
Old 08-13-2007
Lala Lajpathrai
 
Posts: n/a
Default Re: PostgreSQL and select nextval

Hi

Insert command can not be nested. I mean, we can not use another sql
command in Insert statement

__Lala

Reply With Quote
  #4 (permalink)  
Old 08-13-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] PostgreSQL and select nextval

On Sun, August 12, 2007 2:35 am, Alain Roger wrote:
> I'm getting an error message when i run the following SQL request :
> $sql = "INSERT INTO tmp_importedxls (rec_id, publisher) VALUES (SELECT
> nextval('tmp_importedxls_rec_id_seq'),'$pb')";
>
> Error in SQL query: ERROR: syntax error at or near "SELECT" LINE 2:
> VALUES
> (SELECT nextval('tmp_importedxls_rec_id_seq'),' ^
>
> I have the feeling that we can not use the select nextval(...) SQL
> request
> in an INSERT INTO one under PHP.
> Is it true?


No.

PHP doesn't care diddly-squat what is in your query -- It just sends
it to PostgreSQL.

The query you have written just plain won't work in PostgreSQL, period.

Try it in the psql monitor.

OT:
Almost for sure, you just need to strip out the "VALUES (" bit and the
closing paren for it.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
Reply With Quote
  #5 (permalink)  
Old 08-16-2007
Martin Marques
 
Posts: n/a
Default Re: [PHP] PostgreSQL and select nextval

Alain Roger wrote:
> Hi,
>
> I'm getting an error message when i run the following SQL request :
> $sql = "INSERT INTO tmp_importedxls (rec_id, publisher) VALUES (SELECT
> nextval('tmp_importedxls_rec_id_seq'),'$pb')";
>
> Error in SQL query: ERROR: syntax error at or near "SELECT" LINE 2: VALUES
> (SELECT nextval('tmp_importedxls_rec_id_seq'),' ^


You don't use SELECT, just put nextval(...)

INSERT INTO tmp_importedxls (rec_id, publisher) VALUES
(nextval('tmp_importedxls_rec_id_seq'),'$pb')

Luck!

--
21:50:04 up 2 days, 9:07, 0 users, load average: 0.92, 0.37, 0.18
---------------------------------------------------------
Lic. Martín Marqués | SELECT 'mmarques' ||
Centro de Telemática | '@' || 'unl.edu.ar';
Universidad Nacional | DBA, Programador,
del Litoral | Administrador
---------------------------------------------------------
Reply With Quote
  #6 (permalink)  
Old 08-16-2007
Martin Marques
 
Posts: n/a
Default Re: [PHP] PostgreSQL and select nextval

Richard Lynch wrote:
> On Sun, August 12, 2007 2:35 am, Alain Roger wrote:
>> I'm getting an error message when i run the following SQL request :
>> $sql = "INSERT INTO tmp_importedxls (rec_id, publisher) VALUES (SELECT
>> nextval('tmp_importedxls_rec_id_seq'),'$pb')";
>>
>> Error in SQL query: ERROR: syntax error at or near "SELECT" LINE 2:
>> VALUES
>> (SELECT nextval('tmp_importedxls_rec_id_seq'),' ^
>>
>> I have the feeling that we can not use the select nextval(...) SQL
>> request
>> in an INSERT INTO one under PHP.
>> Is it true?

>
> No.
>
> PHP doesn't care diddly-squat what is in your query -- It just sends
> it to PostgreSQL.
>
> The query you have written just plain won't work in PostgreSQL, period.
>
> Try it in the psql monitor.
>
> OT:
> Almost for sure, you just need to strip out the "VALUES (" bit and the
> closing paren for it.


Nop. If you don't put the "VALUES (" you are passing a query with
applyable values for the table in the INSERT.

He only wants the next value in the sequence. In that case he just has
to not put the SELECT.

--
21:50:04 up 2 days, 9:07, 0 users, load average: 0.92, 0.37, 0.18
---------------------------------------------------------
Lic. Martín Marqués | SELECT 'mmarques' ||
Centro de Telemática | '@' || 'unl.edu.ar';
Universidad Nacional | DBA, Programador,
del Litoral | Administrador
---------------------------------------------------------
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:48 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0