The question is old, but I felt that the best answer has not yet been given.
Is there an UPDATE syntax ... without specifying column names ?
General solution with dynamic SQL
You do not need to know the column names, except for some unique columns to join ( id in the example). Works well for any possible corner event that I can think of.
This applies to PostgreSQL. I am creating dynamic code based on information_schema , in particular the information_schema.columns table, which is defined in ANSI SQL, and most modern RDBMS (except Oracle) support it. But DO with PL / pgSQL code that executes dynamic SQL is the completely non-standard PostgreSQL syntax.
DO $do$ BEGIN EXECUTE ( SELECT 'UPDATE b SET (' || string_agg(quote_ident(column_name), ',') || ') = (' || string_agg('a.' || quote_ident(column_name), ',') || ') FROM a WHERE b.id = 123 AND a.id = b.id' FROM information_schema.columns WHERE table_name = 'a' -- table name, case sensitive AND table_schema = 'public' -- schema name, case sensitive AND column_name <> 'id' -- all columns except id ); END $do$;
Assuming a corresponding column in b for each column in a , but not vice versa. b may have additional columns.
WHERE b.id = 123 is optional to update only the selected row.
SQL Fiddle
Related answers with lots of explanation:
Partial Simple SQL Solutions
With a list of shared columns
You still need to know the list of column names that separate both tables. With a syntax shortcut to update multiple columns - shorter than the other answers offered so far anyway.
UPDATE b SET ( column1, column2, column3) = (a.column1, a.column2, a.column3) FROM a WHERE b.id = 123
SQL Fiddle
This syntax was introduced with Postgres 8.2 in December 2006, long before the question was asked.
More details in the manual and this answer on dba.SE:
With a list of columns in b
If all columns are defined a NOT NULL (but not necessarily b ),
and you know the column names b (but not necessarily a ).
UPDATE b SET (column1, column2, column3, column4) = (COALESCE(ab.column1, b.column1) , COALESCE(ab.column2, b.column2) , COALESCE(ab.column3, b.column3) , COALESCE(ab.column4, b.column4) ) FROM ( SELECT * FROM a NATURAL LEFT JOIN b
NATURAL LEFT JOIN row from b , where all columns with the same name have the same value. We do not need an update in this case (nothing changes) and can eliminate these lines at the beginning of the process ( WHERE b.id IS NULL ).
We still need to find a matching string, so b.id = ab.id in the outer query.
SQL Fiddle
This is standard SQL except for the FROM . It works no matter which of the columns is actually present in a , but the query cannot distinguish between the actual NULL values ββand the missing columns in a , so it is only reliable if all the columns are defined in a NOT NULL .
There are several possible options, depending on what you know about all the tables.