I want to insert into a table (circuit) using a select which takes values from 2 tables (segment and wgs). My query:
INSERT INTO circuit (id_circuit, descrip
This constructs an anonymous composite value:
select (1, 'a');
For example:
=> select (1, 'a');
row
-------
(1,a)
(1 row)
=> select row(1, 'a');
row
-------
(1,a)
(1 row)
Note that that is a single composite value, not multiple values.
From the fine manual:
8.16.2. Composite Value Input
To write a composite value as a literal constant, enclose the field values within parentheses and separate them by commas. You can put double quotes around any field value, and must do so if it contains commas or parentheses.
[...]
TheROW
expression syntax can also be used to construct composite values. In most cases this is considerably simpler to use than the string-literal syntax since you don't have to worry about multiple layers of quoting. We already used this method above:ROW('fuzzy dice', 42, 1.99) ROW('', 42, NULL)
The
ROW
keyword is actually optional as long as you have more than one field in the expression, so these can simplify to:('fuzzy dice', 42, 1.99) ('', 42, NULL)
The Row Constructors section might also be of interest.
When you say this:
INSERT INTO circuit (id_circuit, description, date_start, date_end, speed,
length, duration)
SELECT (...)
FROM segment seg, wgs cir where seg.id = 13077
your SELECT
clause only has one column as the whole (...)
expression represents a single value. The solution is to simply drop those parentheses:
INSERT INTO circuit (id_circuit, description, date_start, date_end, speed, length, duration)
SELECT seg.id_segment, ..., (seg.date_end - seg.date_start)
FROM segment seg, wgs cir where seg.id = 13077