Cannot SELECT from UPDATE RETURNING clause in postgres

前端 未结 7 1040
旧时难觅i
旧时难觅i 2020-12-03 10:36

I isolatet the problem from a much more complex query. Here the test scenario

DROP TABLE test; 
CREATE TABLE test (
  id integer,
  description varchar(100)
         


        
相关标签:
7条回答
  • 2020-12-03 11:24

    You update two rows in your UPDATE query, add a WHERE clause to restrict the rows affected.

    UPDATE test SET description = 'test' WHERE id = 1 RETURNING id
    

    to return a single row.

    0 讨论(0)
  • 2020-12-03 11:26

    Are you missing an IN: ... WHERE id IN (UPDATE ...?

    However if I only run the statemennt "UPDATE test set value='test' RETURNING id", I get a result with 2 rows. Why is that?

    Your UPDATE has no WHERE clause and therefore it updates every row, of which there are two.

    0 讨论(0)
  • 2020-12-03 11:35
    UPDATE test set description='test' RETURNING *
    

    would give you the result set that you expect from the initial query.

    But I suspect you were trying something more complex?

    0 讨论(0)
  • 2020-12-03 11:39

    Before PostgreSQL 9.1 INSERT/UPDATE/DELETE could only be used as top level statements. This is why you are getting a syntax error.

    Starting from 9.1 you can use data-modifying statements with common table expressions. Your example query would look like this:

    WITH updated AS (UPDATE test SET description = 'test' RETURNING id)
    SELECT * FROM test WHERE id IN (SELECT id FROM updated);
    

    Be careful with selecting from the just modified table. You can get confusing results that way. Becuse the queries are executed in the same snapshot, the SELECT will not see the effects of the UPDATE statement.

    0 讨论(0)
  • 2020-12-03 11:39
    DROP TABLE IF EXISTS test_tab;
    
    CREATE TABLE test_tab (
      id integer,
      description varchar(100)
    );
    
    INSERT INTO test_tab(id, description) VALUES (1,'new'); 
    INSERT INTO test_tab(id, description) VALUES (2,'new'); 
    
    SELECT * from test_tab;
    
    DO $$
    DECLARE
        myID    test_tab.id%TYPE;
        testID  test_tab.id%TYPE;
        cur_IDs CURSOR for select id from test_tab;
    BEGIN
        OPEN cur_IDs;
        LOOP
            FETCH cur_IDs into testID;
            EXIT WHEN testID is NULL;
    
            UPDATE test_tab SET description='test' WHERE id = testID RETURNING id into myID;
            raise notice 'myID %', myID;
        END LOOP;
        CLOSE cur_IDs;
    END$$;
    
    
    DROP TABLE IF EXISTS test_tab;
    
    0 讨论(0)
  • 2020-12-03 11:41

    You are not limiting your where clause. You need to have id = (blahblah) or id IN (blahblah)

    0 讨论(0)
提交回复
热议问题