Skip to content

Commit 6e8596a

Browse files
committed
Add UPDATE tab SET ROW (col, ...) = (val, ...) for updating
multiple columns Susanne Ebrecht
1 parent 9e522d0 commit 6e8596a

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

doc/src/sgml/ref/update.sgml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.38 2006/08/12 02:52:03 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.39 2006/09/02 20:34:47 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -21,7 +21,8 @@ PostgreSQL documentation
2121
<refsynopsisdiv>
2222
<synopsis>
2323
UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ [ AS ] <replaceable class="parameter">alias</replaceable> ]
24-
SET <replaceable class="PARAMETER">column</replaceable> = { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...]
24+
[ SET <replaceable class="PARAMETER">column</replaceable> = { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...] |
25+
SET ( <replaceable class="PARAMETER">column</replaceable> [, ...] ) = ( { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...] ) [, ...] ]
2526
[ FROM <replaceable class="PARAMETER">fromlist</replaceable> ]
2627
[ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
2728
[ RETURNING * | <replaceable class="parameter">output_expression</replaceable> [ AS <replaceable class="parameter">output_name</replaceable> ] [, ...] ]
@@ -250,6 +251,10 @@ UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
250251
<programlisting>
251252
UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
252253
WHERE city = 'San Francisco' AND date = '2003-07-03';
254+
</programlisting>
255+
<programlisting>
256+
UPDATE weather SET (temp_lo, temp_hi, prcp) = (temp_lo+1, temp_lo+15, DEFAULT)
257+
WHERE city = 'San Francisco' AND date = '2003-07-03';
253258
</programlisting>
254259
</para>
255260

src/backend/parser/gram.y

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.560 2006/09/02 18:17:17 momjian Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.561 2006/09/02 20:34:47 momjian Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -237,7 +237,8 @@ static void doNegateFloat(Value *v);
237237
name_list from_clause from_list opt_array_bounds
238238
qualified_name_list any_name any_name_list
239239
any_operator expr_list attrs
240-
target_list update_target_list insert_column_list
240+
target_list update_col_list update_target_list
241+
update_value_list insert_column_list
241242
values_list def_list indirection opt_indirection
242243
group_clause TriggerFuncArgs select_limit
243244
opt_select_limit opclass_item_list
@@ -308,7 +309,8 @@ static void doNegateFloat(Value *v);
308309
%type <jexpr> joined_table
309310
%type <range> relation_expr
310311
%type <range> relation_expr_opt_alias
311-
%type <target> target_el update_target_el insert_column_item
312+
%type <target> target_el update_target_el update_col_list_el insert_column_item
313+
%type <list> update_target_lists_list update_target_lists_el
312314

313315
%type <typnam> Typename SimpleTypename ConstTypename
314316
GenericType Numeric opt_float
@@ -5537,6 +5539,20 @@ UpdateStmt: UPDATE relation_expr_opt_alias
55375539
n->returningList = $7;
55385540
$$ = (Node *)n;
55395541
}
5542+
| UPDATE relation_expr_opt_alias
5543+
SET update_target_lists_list
5544+
from_clause
5545+
where_clause
5546+
returning_clause
5547+
{
5548+
UpdateStmt *n = makeNode(UpdateStmt);
5549+
n->relation = $2;
5550+
n->targetList = $4;
5551+
n->fromClause = $5;
5552+
n->whereClause = $6;
5553+
n->returningList = $7;
5554+
$$ = (Node *)n;
5555+
}
55405556
;
55415557

55425558

@@ -5941,6 +5957,60 @@ values_item:
59415957
| DEFAULT { $$ = (Node *) makeNode(SetToDefault); }
59425958
;
59435959

5960+
update_target_lists_list:
5961+
update_target_lists_el { $$ = $1; }
5962+
| update_target_lists_list ',' update_target_lists_el { $$ = list_concat($1, $3); }
5963+
;
5964+
5965+
update_target_lists_el:
5966+
'(' update_col_list ')' '=' '(' update_value_list ')'
5967+
{
5968+
ListCell *col_cell;
5969+
ListCell *val_cell;
5970+
5971+
if (list_length($2) != list_length($6))
5972+
{
5973+
ereport(ERROR,
5974+
(errcode(ERRCODE_SYNTAX_ERROR),
5975+
errmsg("number of columns does not match to number of values")));
5976+
}
5977+
5978+
for (col_cell = list_head($2), val_cell = list_head($6);
5979+
col_cell != NULL && val_cell != NULL;
5980+
col_cell = lnext(col_cell), val_cell = lnext(val_cell))
5981+
{
5982+
/* merge update_value_list with update_col_list */
5983+
ResTarget *res_col = (ResTarget *) lfirst(col_cell);
5984+
ResTarget *res_val = (ResTarget *) lfirst(val_cell);
5985+
5986+
res_col->val = (Node *)copyObject(res_val);
5987+
}
5988+
5989+
$$ = $2;
5990+
}
5991+
;
5992+
5993+
update_col_list:
5994+
update_col_list_el { $$ = list_make1($1); }
5995+
| update_col_list ',' update_col_list_el { $$ = lappend($1, $3); }
5996+
;
5997+
5998+
update_col_list_el:
5999+
ColId opt_indirection
6000+
{
6001+
$$ = makeNode(ResTarget);
6002+
$$->name = $1;
6003+
$$->indirection = $2;
6004+
$$->val = NULL;
6005+
$$->location = @1;
6006+
}
6007+
;
6008+
6009+
update_value_list:
6010+
values_item { $$ = list_make1($1); }
6011+
| update_value_list ',' values_item { $$ = lappend($1, $3); }
6012+
;
6013+
59446014

59456015
/*****************************************************************************
59466016
*
@@ -8253,7 +8323,7 @@ target_el: a_expr AS ColLabel
82538323
;
82548324

82558325
update_target_list:
8256-
update_target_el { $$ = list_make1($1); }
8326+
update_target_el { $$ = list_make1($1); }
82578327
| update_target_list ',' update_target_el { $$ = lappend($1,$3); }
82588328
;
82598329

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy