我正在尝试从游戏数据库中删除与用户 ID 相关的所有数据。
有一张桌子,里面放着所有游戏(每个游戏由 3 名玩家玩):
# select * from pref_games where gid=321;
gid | rounds | finished
-----+--------+----------------------------
321 | 17 | 2011-10-26 17:16:04.074402
(1 row)
并且有一个表格包含该游戏#321的玩家得分:
# select * from pref_scores where gid=321;
id | gid | money | quit
----------------+-----+-------+------
OK531282114947 | 321 | 218 | f
OK501857527071 | 321 | -156 | f
OK429671947957 | 321 | -62 | f
当我在 PostgreSQL 的 psql-prompt 上尝试以下 SELECT INTO 语句时,它似乎按预期工作(并且会话关闭时临时表消失):
# select gid into temp temp_gids from pref_scores where id='OK446163742289';
SELECT
# select * from temp_gids ;
gid
------
1895
1946
1998
2094
2177
2215
(6 rows)
但是当我尝试创建我的 PL/pgSQL 过程时,我得到了错误:
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;
错误:
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
为什么会这样(这里不允许使用临时表?)以及在哪里保存要删除的 gid 的临时列表?
(而且我不想使用“on delete cascade”,因为我还不习惯它,而且我的脚本/数据库还没有为此做好准备)。