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 8562743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:47:49+00:00 2026-06-11T16:47:49+00:00

I am attempting to write a PL/PgSQL function which will execute a query stored

  • 0

I am attempting to write a PL/PgSQL function which will execute a query stored within a PostgreSQL table like so:

CREATE OR REPLACE FUNCTION evaluate_scenario(scenario_id int)
RETURNS TABLE(line_item_id int, organization_id int, data_element_id int, value varchar) AS $$
BEGIN
  RETURN QUERY
  SELECT
    li.id,
    li.organization_id,
    de.id,
    (EXECUTE 'SELECT ' || de.query)::varchar
  FROM
    line_items AS li INNER JOIN
    summary_files AS sf ON li.summary_file_id = sf.id INNER JOIN
    scenarios AS s ON s.summary_file_id = sf.id CROSS JOIN
    data_elements AS de
  WHERE
    s.id = 1 AND
    de.scope = 3 AND (
      de.model_id = s.model_id OR
      de.scenario_id = s.id OR
      de.organization_id = s.organization_id
    );
END;
$$ LANGUAGE plpgsql;

When I try to run this query:

select line_item_id, data_element_id, value from evaluate_scenario(1)

I receive the following error:

********** Error **********

ERROR: type "execute" does not exist
SQL state: 42704
Context: PL/pgSQL function "evaluate_scenario" line 3 at RETURN QUERY

Any help would be greatly appreciated. Note that if I remove the “EXECUTE” and just do SELECT de.query inside those parens, I receive the value for that column as expected, but I can’t figure out how to make de.query an acceptable query string for “EXECUTE”.

  • 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-11T16:47:50+00:00Added an answer on June 11, 2026 at 4:47 pm

    EXECUTE takes a query string as parameter and executes it, but you cannot nest EXECUTE in a query.

    There are a couple of variants for EXECUTE in plpgsql. For your case, RETURN QUERY EXECUTE query might be best. Read the manual.

    This could work – I am not trying to fix all the mess, just demonstrate a working example.

    CREATE OR REPLACE FUNCTION evaluate_scenario(_scenario_id int)
    RETURNS TABLE(line_item_id int, organization_id int, data_element_id int, value varchar) AS
    $func$
    
    BEGIN
      RETURN QUERY EXECUTE '
      SELECT li.id,
             li.organization_id,
             de.id,
            (SELECT '
    || (SELECT query
        FROM   data_elements
        WHERE  scenario_id = _scenario_id)
    || ')
      FROM   line_items AS li
      JOIN   summary_files AS sf ON li.summary_file_id = sf.id
      JOIN   scenarios AS s ON s.summary_file_id = sf.id
      CROSS  JOIN data_elements AS de
      WHERE  s.id = 1
      AND    de.scope = 3
      AND   (de.model_id = s.model_id OR
             de.scenario_id = s.id OR
             de.organization_id = s.organization_id
            )';
    
    END;
    $func$ LANGUAGE plpgsql;
    

    Note that it is inherently unsafe to execute text literals as code. If someone can smuggle a DROP * FROM tbl or some such into the table data_elements, you are in big trouble. I am speaking of SQL injection.
    Read more about SQLi at bobby-tables.com.

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

Sidebar

Related Questions

I am attempting to write a function which will operate on a std::map of
I am attempting to write an Android app which will allow me to read
I am attempting to write a interpreter that will turn a string like: vector(random(0,
I'm attempting to write a SQL search which will allow return any records which
I'm attempting to write a function which starts a thread for each contact and
I'm attempting to write a function in assembly that will detect if a longer
I'm attempting to write a query that will return The most recent AccountDate with
I'm attempting to write a query that will return any customer that has multiple
I am attempting to write to Azure Table Storage without using the TableContext and
I'm attempting to write a TinyURL like clone in ASP.NET MVC as a first

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.