2003-08-26 20:32:23 +02:00
|
|
|
--
|
2006-09-04 00:37:06 +02:00
|
|
|
-- UPDATE syntax tests
|
2003-08-26 20:32:23 +02:00
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE update_test (
|
|
|
|
a INT DEFAULT 10,
|
2006-09-04 00:37:06 +02:00
|
|
|
b INT,
|
|
|
|
c TEXT
|
2003-08-26 20:32:23 +02:00
|
|
|
);
|
|
|
|
|
2006-09-04 00:37:06 +02:00
|
|
|
INSERT INTO update_test VALUES (5, 10, 'foo');
|
|
|
|
INSERT INTO update_test(b, a) VALUES (15, 10);
|
2003-08-26 20:32:23 +02:00
|
|
|
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
|
|
|
|
UPDATE update_test SET a = DEFAULT, b = DEFAULT;
|
|
|
|
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
|
2006-01-22 06:20:35 +01:00
|
|
|
-- aliases for the UPDATE target table
|
|
|
|
UPDATE update_test AS t SET b = 10 WHERE t.a = 10;
|
|
|
|
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
|
|
|
|
UPDATE update_test t SET b = t.b + 10 WHERE t.a = 10;
|
|
|
|
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
|
2006-08-03 16:54:44 +02:00
|
|
|
--
|
|
|
|
-- Test VALUES in FROM
|
|
|
|
--
|
|
|
|
|
|
|
|
UPDATE update_test SET a=v.i FROM (VALUES(100, 20)) AS v(i, j)
|
2006-09-04 00:37:06 +02:00
|
|
|
WHERE update_test.b = v.j;
|
2006-08-03 16:54:44 +02:00
|
|
|
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
|
2006-09-04 00:37:06 +02:00
|
|
|
--
|
|
|
|
-- Test multiple-set-clause syntax
|
|
|
|
--
|
|
|
|
|
Implement UPDATE tab SET (col1,col2,...) = (SELECT ...), ...
This SQL-standard feature allows a sub-SELECT yielding multiple columns
(but only one row) to be used to compute the new values of several columns
to be updated. While the same results can be had with an independent
sub-SELECT per column, such a workaround can require a great deal of
duplicated computation.
The standard actually says that the source for a multi-column assignment
could be any row-valued expression. The implementation used here is
tightly tied to our existing sub-SELECT support and can't handle other
cases; the Bison grammar would have some issues with them too. However,
I don't feel too bad about this since other cases can be converted into
sub-SELECTs. For instance, "SET (a,b,c) = row_valued_function(x)" could
be written "SET (a,b,c) = (SELECT * FROM row_valued_function(x))".
2014-06-18 19:22:25 +02:00
|
|
|
INSERT INTO update_test SELECT a,b+1,c FROM update_test;
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
|
2006-09-04 00:37:06 +02:00
|
|
|
UPDATE update_test SET (c,b,a) = ('bugle', b+11, DEFAULT) WHERE c = 'foo';
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
UPDATE update_test SET (c,b) = ('car', a+b), a = a + 1 WHERE a = 10;
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
-- fail, multi assignment to same column:
|
|
|
|
UPDATE update_test SET (c,b) = ('car', a+b), b = a + 1 WHERE a = 10;
|
|
|
|
|
Implement UPDATE tab SET (col1,col2,...) = (SELECT ...), ...
This SQL-standard feature allows a sub-SELECT yielding multiple columns
(but only one row) to be used to compute the new values of several columns
to be updated. While the same results can be had with an independent
sub-SELECT per column, such a workaround can require a great deal of
duplicated computation.
The standard actually says that the source for a multi-column assignment
could be any row-valued expression. The implementation used here is
tightly tied to our existing sub-SELECT support and can't handle other
cases; the Bison grammar would have some issues with them too. However,
I don't feel too bad about this since other cases can be converted into
sub-SELECTs. For instance, "SET (a,b,c) = row_valued_function(x)" could
be written "SET (a,b,c) = (SELECT * FROM row_valued_function(x))".
2014-06-18 19:22:25 +02:00
|
|
|
-- uncorrelated sub-select:
|
|
|
|
UPDATE update_test
|
|
|
|
SET (b,a) = (select a,b from update_test where b = 41 and c = 'car')
|
|
|
|
WHERE a = 100 AND b = 20;
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
-- correlated sub-select:
|
|
|
|
UPDATE update_test o
|
|
|
|
SET (b,a) = (select a+1,b from update_test i
|
|
|
|
where i.a=o.a and i.b=o.b and i.c is not distinct from o.c);
|
|
|
|
SELECT * FROM update_test;
|
|
|
|
-- fail, multiple rows supplied:
|
|
|
|
UPDATE update_test SET (b,a) = (select a+1,b from update_test);
|
|
|
|
-- set to null if no rows supplied:
|
|
|
|
UPDATE update_test SET (b,a) = (select a+1,b from update_test where a = 1000)
|
|
|
|
WHERE a = 11;
|
|
|
|
SELECT * FROM update_test;
|
2006-09-04 00:37:06 +02:00
|
|
|
|
2006-01-22 06:20:35 +01:00
|
|
|
-- if an alias for the target table is specified, don't allow references
|
|
|
|
-- to the original table name
|
|
|
|
UPDATE update_test AS t SET b = update_test.b + 10 WHERE t.a = 10;
|
|
|
|
|
2011-04-25 15:46:53 +02:00
|
|
|
-- Make sure that we can update to a TOASTed value.
|
|
|
|
UPDATE update_test SET c = repeat('x', 10000) WHERE c = 'car';
|
|
|
|
SELECT a, b, char_length(c) FROM update_test;
|
|
|
|
|
2005-04-07 17:23:06 +02:00
|
|
|
DROP TABLE update_test;
|