ゲームデータベースからユーザー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で次のSELECTINTOステートメントを試してみると、期待どおりに機能しているようです(セッションを閉じると一時テーブルが消えます)。
# 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の一時リストをどこに保存するのですか?
(そして、私はまだそれに慣れておらず、スクリプト/データベースがまだそのために準備されていないので、「カスケードの削除」を使用したくないです)。