Using temp table in PL/pgSQL procedure for cleaning tables

前端 未结 3 1350
萌比男神i
萌比男神i 2020-12-17 22:29

I\'m trying to delete all data related to a user id from a game database.

There is a table holding all games (each played by 3 players):

# select * f         


        
3条回答
  •  天命终不由人
    2020-12-17 22:55

    You could create the temporary table and then do the usual INSERT ... SELECT as separate operations:

    create temporary table temp_gids (gid int not null) on commit drop;
    insert into temp_gids (gid) select gid from pref_scores where id = _id;
    

    There's also a LIKE option to CREATE TABLE if you want to duplicate a table's structure:

    LIKE parent_table [ like_option ... ]
    The LIKE clause specifies a table from which the new table automatically copies all column names, their data types, and their not-null constraints.

    But I think you just need a temporary table to hold some IDs so that's probably overkill.

    SELECT INTO works as you expect outside a procedure:

    [...] this form of SELECT INTO is not available in ECPG or PL/pgSQL, because they interpret the INTO clause differently.

    SELECT INTO is used to store the result of a SELECT in a local variable inside a PostgreSQL procedure:

    The result of a SQL command yielding a single row (possibly of multiple columns) can be assigned to a record variable, row-type variable, or list of scalar variables. This is done by writing the base SQL command and adding an INTO clause.

提交回复
热议问题