Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9025479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:16:55+00:00 2026-06-16T06:16:55+00:00

I have a function which generates a SELECT query dynamically using some If Else

  • 0

I have a function which generates a SELECT query dynamically using some If Else conditions .I have stored that query in a TEXT type variable as

    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

But with plpgsql(PL/SQL for postgres) I don’t know how to execute this same query in a variable . Please help me with this .
Thanks in Advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:16:56+00:00Added an answer on June 16, 2026 at 6:16 am

    From the docs (Postgres 9.1):

    EXECUTE command-string [ INTO [STRICT] target ] [ USING expression [, ... ] ];
    

    where command-string is an expression yielding a string (of type text) containing the command to be executed. The optional target is a
    record variable, a row variable, or a comma-separated list of simple
    variables and record/row fields, into which the results of the command
    will be stored.

    In other words:

    Move the INTO old_ans_col_val from the String to the place where you EXECUTE it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that generates some standard SQL like SELECT * FROM TEST
Suppose that I have a database query which looks like below - select name,
I have an ajax query which pulls some JSON data and generates me a
I have some designer generated code that I am using to query a dataset.
I have a charting application that dynamically generates SQL Server queries to compute values
Let's assume that we have a PHP class Page , which generates PHP Page
I have some PHP that generates the a JSON object from data from a
I have a page which does a SQL query via PHP and generates a
I have a php function that generates a comma separated list of Post IDs
Hi I have a function that will generate a random string which works fine.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.