gpt4 book ai didi

Postgresql 动态更新查询执行

转载 作者:行者123 更新时间:2023-11-29 13:35:23 25 4
gpt4 key购买 nike

我有一个 plpgsql block ,它生成一个动态更新查询:

 CREATE OR REPLACE FUNCTION fun_lenght_test () RETURNS VOID AS $BODY$
BEGIN
/*
some code here
*/
dyn_sql = 'UPDATE';
IF SUBSTR(que_col_name, 0, 8) = 'pro_ans' THEN
dyn_sql = dyn_sql || ' profile_answers ';
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 ';
END IF;

dyn_sql = dyn_sql|| 'SET' || ' ' || que_col_name || ' = ' || NEW_VALUE_FOR_COLUMN ;

dyn_sql = dyn_sql || ' WHERE' ;

IF SUBSTR(que_col_name, 0, 8) = 'pro_ans' THEN
dyn_sql = dyn_sql || ' pro_ans_frn_pro_id = (SELECT pro_ans_frn_pro_id FROM profile_answers JOIN registrations ON (pro_ans_frn_pro_id = reg_frn_pro_id) WHERE reg_id ';
ELSIF SUBSTR(que_col_name, 0, 8) = 'reg_ans' THEN
dyn_sql = dyn_sql || ' reg_answers WHERE reg_ans_frn_reg_id ';
ELSIF SUBSTR(que_col_name, 0, 8) = 'tvl_ans' THEN
dyn_sql = dyn_sql || ' tvl_answers WHERE tvl_ans_frn_reg_id ';
END IF;

IF SUBSTR(que_col_name, 0, 8) = 'pro_ans' THEN
dyn_sql = dyn_sql || '= ' || CAST(temp_reg AS VARCHAR) || ');';
ELSE
dyn_sql = dyn_sql || '= ' || CAST(temp_reg AS VARCHAR) || ';';

/* want to execute this update dynamic query */
/*
some code here

*/
END;
$BODY$
LANGUAGE plpgsql

你能帮我执行这个动态更新查询吗?提前致谢。

最佳答案

创建或替换函数 fun_lenght_test ()返回无效 $ body $

DECLARE
dyn_sql varchar;
BEGIN
/*
some code here
*/
dyn_sql = 'UPDATE';
IF SUBSTR(que_col_name, 0, 8) = 'pro_ans' THEN
dyn_sql = dyn_sql || ' profile_answers ';
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 ';
END IF;

dyn_sql = dyn_sql|| 'SET' || ' ' || que_col_name || ' = ' || NEW_VALUE_FOR_COLUMN ;

dyn_sql = dyn_sql || ' WHERE' ;

IF SUBSTR(que_col_name, 0, 8) = 'pro_ans' THEN
dyn_sql = dyn_sql || ' pro_ans_frn_pro_id = (SELECT pro_ans_frn_pro_id FROM profile_answers JOIN registrations ON (pro_ans_frn_pro_id = reg_frn_pro_id) WHERE reg_id ';
ELSIF SUBSTR(que_col_name, 0, 8) = 'reg_ans' THEN
dyn_sql = dyn_sql || ' reg_answers WHERE reg_ans_frn_reg_id ';
ELSIF SUBSTR(que_col_name, 0, 8) = 'tvl_ans' THEN
dyn_sql = dyn_sql || ' tvl_answers WHERE tvl_ans_frn_reg_id ';
END IF;

IF SUBSTR(que_col_name, 0, 8) = 'pro_ans' THEN
dyn_sql = dyn_sql || '= ' || CAST(temp_reg AS VARCHAR) || ');';
ELSE
dyn_sql = dyn_sql || '= ' || CAST(temp_reg AS VARCHAR) || ';';
END IF;

结束;$BODY$ LANGUAGE plpgsql

很抱歉知道你想要什么,但这可以运行...

关于Postgresql 动态更新查询执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13967279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com