Search Postgresql Archives

Re: insert into...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>
"Alain Roger" <raf.news@xxxxxxxxx> writes:
> i would like to understand why the following INSERT INTO statement works :

> INSERT INTO mytable
>    SELECT nextval('my_sequence'),
>    'myname',
>    'myfirstname'
> ;

This is a perfectly standard INSERT ... SELECT query.

> whereas usually we should do :

> INSERT INTO mytable
> VALUES
> (
>    SELECT nextval('my_sequence'),
>    'myname',
>    'myfirstname'
> );

If you'd tried that, you would find that it *does not* work:

regression=# INSERT INTO mytable
regression-# VALUES
regression-# (
regression(#    SELECT nextval('my_sequence'),
regression(#    'myname',
regression(#    'myfirstname'
regression(# );
ERROR:  syntax error at or near "SELECT"
LINE 4:    SELECT nextval('my_sequence'),
           ^

You could make it work by turning the SELECT into a parenthesized
sub-SELECT:

INSERT INTO mytable
VALUES
(
   (SELECT nextval('my_sequence')),
   'myname',
   'myfirstname'
);

but this is just pointless complexity.  The standard idiom is

INSERT INTO mytable
VALUES
(
   nextval('my_sequence'),
   'myname',
   'myfirstname'
);

or as already noted, leave out the column entirely and rely on
the default expression.

			regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux