Re: INSERT INTO ... SELECT

From: Christopher Browne <cbbrowne(at)acm(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: INSERT INTO ... SELECT
Date: 2003-08-12 01:57:05
Message-ID: m3k79jhc3y.fsf@chvatal.cbbrowne.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Centuries ago, Nostradamus foresaw when trissl(at)informatik(dot)hu-berlin(dot)de (Silke Trissl) would write:
> I would like to insert into a table values from a table and user
> defined ones. Here is the example:
>
> I found this statement to insert values from another table:
>
> INSERT INTO test_table (cust_id, cust_name) SELECT id, name from CUSTOMER;
>
> But the test_table has another column, which should have the same
> value for all the customers.
>
> Is there something like
>
> INSERT INTO test_table (int_id, cust_id, cust_name) '1', SELECT id,
> name from CUSTOMER:
>
> and if so, what ist the correct statement? If not, what is an
> alternative to insert a single row at a time?

You're close.

The constant term needs to be inside the SELECT.

Try:
insert into test_table (int_id, cust_id, cust_name)
select '1', id, name from customer;
--
select 'cbbrowne' || '@' || 'acm.org';
http://www.ntlug.org/~cbbrowne/sap.html
(eq? 'truth 'beauty) ; to avoid unassigned-var error, since compiled code
; will pick up previous value to var set!-ed,
; the unassigned object.
-- from BBN-CL's cl-parser.scm

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jonathan Gardner 2003-08-12 16:40:01 Re: INSERT INTO ... SELECT
Previous Message Tom Lane 2003-08-11 23:12:44 Re: string function -- replace needs a clear doc