UPDATE Syntax

Started by Ulrich Goebelabout 5 years ago2 messagesgeneral
Jump to latest
#1Ulrich Goebel
ml@fam-goebel.de

Hi,

in a Python Script I build an UPDATE using the syntax:

update tbl set (col1, col2, ...) = (val1, val2, ...) where id = xx

That works as long in the two lists are more then one column and values.
If I have just one column and value, I get an error message:

==========================================================================
cg=# select id, name, vorname, status from tbl_person;
id | name | vorname | status
----+--------+---------+--------
3 | Goebel | Ulrich | a
(1 row)

cg=#
cg=#
cg=#
cg=# update tbl_person set status = 'a' where id=3;
UPDATE 1
cg=#
cg=#
cg=#
cg=# update tbl_person set (status) = ('a') where id=3;
ERROR: source for a multiple-column UPDATE item must be a sub-SELECT or
ROW() expression
LINE 1: update tbl_person set (status) = ('a') where id=3;
^
cg=#
cg=#
cg=#
cg=# update tbl_person set (status, name) = ('a', 'Goebel') where id=3;
UPDATE 1
cg=#
==========================================================================

In fact in the script I have both cases: one ore more columns.

Is there another way but to code a case differentiation to avoid this
error message? Well, I could use the other syntax:

update tbl set col1=val1, col2=val2, ... where id=xx

but I just like the former...

Thank's
Ulrich

--
Ulrich Goebel
Am Büchel 57, 53173 Bonn

#2Ulrich Goebel
ml@fam-goebel.de
In reply to: Ulrich Goebel (#1)
Re: UPDATE Syntax - solved

Solved. Use

update tbl set (col1, col2, ...) = ROW(val1, val2, ...) where id=xx

Thank's to Laurenz Albe in the german mailinglist.

Am 03.02.21 um 11:31 schrieb Ulrich Goebel:

Hi,

in a Python Script I build an UPDATE using the syntax:

update tbl set (col1, col2, ...) = (val1, val2, ...) where id = xx

That works as long in the two lists are more then one column and values.
If I have just one column and value, I get an error message:

==========================================================================
cg=# select id, name, vorname, status from tbl_person;
 id |  name  | vorname | status
----+--------+---------+--------
  3 | Goebel | Ulrich  | a
(1 row)

cg=#
cg=#
cg=#
cg=# update tbl_person set status = 'a' where id=3;
UPDATE 1
cg=#
cg=#
cg=#
cg=# update tbl_person set (status) = ('a') where id=3;
ERROR:  source for a multiple-column UPDATE item must be a sub-SELECT or
ROW() expression
LINE 1: update tbl_person set (status) = ('a') where id=3;
                                          ^
cg=#
cg=#
cg=#
cg=# update tbl_person set (status, name) = ('a', 'Goebel') where id=3;
UPDATE 1
cg=#
==========================================================================

In fact in the script I have both cases: one ore more columns.

Is there another way but to code a case differentiation to avoid this
error message? Well, I could use the other syntax:

update tbl set col1=val1, col2=val2, ... where id=xx

but I just like the former...

Thank's
Ulrich

--
Ulrich Goebel
Am Büchel 57, 53173 Bonn