1

動的更新クエリを次のように生成する plpgsql ブロッ​​クがあります。

 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

この動的更新クエリの実行を手伝っていただけますか。前もって感謝します。

4

1 に答える 1

1

関数の作成または置換fun_lenght_test()は、VOIDを$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

何が欲しいのか申し訳ありませんが、これは実行できます...

于 2012-12-20T07:59:53.390 に答える