Delete rows of a table specified in a text file in Postgres

前端 未结 1 1995
生来不讨喜
生来不讨喜 2021-01-13 22:59

I have a text file containing the row numbers of the rows that should be deleted in my table like this:

3
32
40
55
[...]

How can I get a Po

相关标签:
1条回答
  • 2021-01-13 23:33

    Doing it once could look like this:

    CREATE TEMP TABLE tmp_x (nr int);
    
    COPY tmp_x FROM '/absolute/path/to/file';
    
    DELETE FROM mytable d
     USING tmp_x
     WHERE d.mycol = tmp_x.nr;
    
    DROP TABLE tmp_x;
    

    For repeated use, wrap it into a plpgsql function with file-path / table name / column name as parameters. If table or column name are dynamic you have to use EXECUTE for the DELETE.

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