I am trying to delete all data related to user id from game database.
There is a table containing all the games (each of which consists of 3 players):
# select * from pref_games where gid=321; gid | rounds | finished -----+--------+---------------------------- 321 | 17 | 2011-10-26 17:16:04.074402 (1 row)
And there is a table in which players play for game # 321:
# select * from pref_scores where gid=321; id | gid | money | quit ----------------+-----+-------+------ OK531282114947 | 321 | 218 | f OK501857527071 | 321 | -156 | f OK429671947957 | 321 | -62 | f
When I try to execute the following SELECT INTO statement at the PostgreSQL psql prompt, it works as expected (and the temp table disappears when the session closes):
# select gid into temp temp_gids from pref_scores where id='OK446163742289'; SELECT
But when I try to create my PL / pgSQL procedure, I get an error:
create or replace function pref_delete_user(_id varchar) returns void as $BODY$ begin select gid into temp temp_gids from pref_scores where id=_id; delete from pref_scores where gid in (select gid from temp_gids); delete from pref_games where gid in (select gid from temp_gids); delete from pref_rep where author=_id; delete from pref_rep where id=_id; delete from pref_catch where id=_id; delete from pref_game where id=_id; delete from pref_hand where id=_id; delete from pref_luck where id=_id; delete from pref_match where id=_id; delete from pref_misere where id=_id; delete from pref_money where id=_id; delete from pref_pass where id=_id; delete from pref_status where id=_id; delete from pref_users where id=_id; end; $BODY$ language plpgsql;
Mistake:
ERROR: syntax error at "temp" DETAIL: Expected record variable, row variable, or list of scalar variables following INTO. CONTEXT: compilation of PL/pgSQL function "pref_delete_user" near line 3
Why is this (temporary tables not allowed here?) And where to save my list of topics from the gid to be deleted?
(And I would prefer not to use "to remove the cascade", because I'm not used to it yet, and my scripts / database is not prepared for this yet).
Alexander farber
source share