update.out 2.3 KB
Newer Older
1
--
2
-- UPDATE syntax tests
3 4 5
--
CREATE TABLE update_test (
    a   INT DEFAULT 10,
6 7
    b   INT,
    c   TEXT
8
);
9 10
INSERT INTO update_test VALUES (5, 10, 'foo');
INSERT INTO update_test(b, a) VALUES (15, 10);
11
SELECT * FROM update_test;
12 13 14 15
 a  | b  |  c  
----+----+-----
  5 | 10 | foo
 10 | 15 | 
16 17 18 19
(2 rows)

UPDATE update_test SET a = DEFAULT, b = DEFAULT;
SELECT * FROM update_test;
20 21 22 23
 a  | b |  c  
----+---+-----
 10 |   | foo
 10 |   | 
24 25
(2 rows)

26 27 28
-- aliases for the UPDATE target table
UPDATE update_test AS t SET b = 10 WHERE t.a = 10;
SELECT * FROM update_test;
29 30 31 32
 a  | b  |  c  
----+----+-----
 10 | 10 | foo
 10 | 10 | 
33 34 35 36
(2 rows)

UPDATE update_test t SET b = t.b + 10 WHERE t.a = 10;
SELECT * FROM update_test;
37 38 39 40
 a  | b  |  c  
----+----+-----
 10 | 20 | foo
 10 | 20 | 
41 42
(2 rows)

43 44 45 46
--
-- Test VALUES in FROM
--
UPDATE update_test SET a=v.i FROM (VALUES(100, 20)) AS v(i, j)
47
  WHERE update_test.b = v.j;
48
SELECT * FROM update_test;
49 50 51 52
  a  | b  |  c  
-----+----+-----
 100 | 20 | foo
 100 | 20 | 
53 54
(2 rows)

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
--
-- Test multiple-set-clause syntax
--
UPDATE update_test SET (c,b,a) = ('bugle', b+11, DEFAULT) WHERE c = 'foo';
SELECT * FROM update_test;
  a  | b  |   c   
-----+----+-------
 100 | 20 | 
  10 | 31 | bugle
(2 rows)

UPDATE update_test SET (c,b) = ('car', a+b), a = a + 1 WHERE a = 10;
SELECT * FROM update_test;
  a  | b  |  c  
-----+----+-----
 100 | 20 | 
  11 | 41 | car
(2 rows)

-- fail, multi assignment to same column:
UPDATE update_test SET (c,b) = ('car', a+b), b = a + 1 WHERE a = 10;
ERROR:  multiple assignments to same column "b"
-- XXX this should work, but doesn't yet:
UPDATE update_test SET (a,b) = (select a,b FROM update_test where c = 'foo')
  WHERE a = 10;
ERROR:  syntax error at or near "select"
LINE 1: UPDATE update_test SET (a,b) = (select a,b FROM update_test ...
                                        ^
83 84 85 86 87 88
-- if an alias for the target table is specified, don't allow references
-- to the original table name
BEGIN;
SET LOCAL add_missing_from = false;
UPDATE update_test AS t SET b = update_test.b + 10 WHERE t.a = 10;
ERROR:  invalid reference to FROM-clause entry for table "update_test"
89 90
LINE 1: UPDATE update_test AS t SET b = update_test.b + 10 WHERE t.a...
                                        ^
91 92
HINT:  Perhaps you meant to reference the table alias "t".
ROLLBACK;
93
DROP TABLE update_test;