How to write a Postgres UPDATE query with parameters specified as a list of values

835 Views Asked by At

I'm trying to write a Postgres UPDATE query with parameters specified as a list of values, but I'm getting a syntax error. However very similar INSERT query works just fine.

Query giving an error

'update user set (username, firstname, lastname) values($1, $2, $3) where id = 3'


Similar query that works fine

'insert into user (id, position, firstname, lastname) values($1, $2, $3, $4) '
1

There are 1 best solutions below

1
hermit On BEST ANSWER

The correct syntax is:

update user set (username, firstname, lastname) = ($1, $2, $3) where id = 3

This is called column-list syntax of update statement in PostgreSQL.