我有一个函数,它使用一些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)我不知道如何在变量中执行相同的查询。请帮我解决一下这个 。 在此先感谢
答案 0 :(得分:2)
来自文档(Postgres 9.1):
EXECUTE command-string [ INTO [STRICT] target ] [ USING expression [, ... ] ];
其中command-string是一个表达式,表示包含要执行的命令的字符串(text类型)。可选目标是a 记录变量,行变量或逗号分隔的简单列表 变量和记录/行字段,命令的结果 将被存储。
换句话说:
将INTO old_ans_col_val
从字符串移动到EXECUTE
的位置。
答案 1 :(得分:1)