If Else 条件を使用して SELECT クエリを動的に生成する関数があります。そのクエリを TEXT 型変数に次のように格納しました。
CREATE OR REPLACE FUNCTION func_updateanswercodes(ans_id_param BIGINT,
que_id_param BIGINT,
overwrite_param INTEGER,
new_ans_code_param CHARACTER VARYING)
RETURNS INTEGER AS
$BODY$
...................
................
...................
dyn_sql = 'SELECT ' || que_col_name || ' INTO old_ans_col_val FROM';
IF SUBSTR(que_col_name, 0, 8) = 'pro_ans' THEN
dyn_sql = dyn_sql || ' profile_answers JOIN registrations ON (pro_ans_frn_pro_id = reg_frn_pro_id)';
ELSIF SUBSTR(que_col_name, 0, 8) = 'reg_ans' THEN
dyn_sql = dyn_sql || ' reg_answers ';
ELSIF SUBSTR(que_col_name, 0, 8) = 'tvl_ans' THEN
dyn_sql = dyn_sql || ' tvl_answers '; --35
END IF;
dyn_sql = dyn_sql || ' WHERE';
IF SUBSTR(que_col_name, 0, 8) = 'pro_ans' THEN
dyn_sql = dyn_sql || ' reg_id ';
ELSIF SUBSTR(que_col_name, 0, 8) = 'reg_ans' THEN
dyn_sql = dyn_sql || ' reg_ans_frn_reg_id ';
ELSIF SUBSTR(que_col_name, 0, 8) = 'tvl_ans' THEN
dyn_sql = dyn_sql || ' tvl_ans_frn_reg_id ';
END IF;
dyn_sql = dyn_sql || '= ' || CAST(temp_reg AS VARCHAR) ||';'
/* Here want to execute that query in variable dync_sql
...........................
.............................
.......................
END;
$BODY$
LANGUAGE plpgsql VOLATILE
しかし、plpgsql(postgresのPL/SQL)では、この同じクエリを variable で実行する方法がわかりません。これで私を助けてください。前もって感謝します