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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:13:11+00:00 2026-06-09T23:13:11+00:00

How can I write a stored procedure that contains a dynamically built SQL statement

  • 0

How can I write a stored procedure that contains a dynamically built SQL statement that returns a result set? Here is my sample code:

CREATE OR REPLACE FUNCTION reporting.report_get_countries_new (
  starts_with varchar,
  ends_with varchar
)
RETURNS TABLE (
  country_id integer,
  country_name varchar
) AS
$body$
DECLARE
  starts_with ALIAS FOR $1;
  ends_with ALIAS FOR $2;
  sql VARCHAR;
BEGIN

    sql = 'SELECT * FROM lookups.countries WHERE lookups.countries.country_name >= ' || starts_with ;

    IF ends_with IS NOT NULL THEN
        sql = sql || ' AND lookups.countries.country_name <= ' || ends_with ;
    END IF;

    RETURN QUERY EXECUTE sql;

END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100 ROWS 1000;

This code returns an error:

ERROR:  syntax error at or near "RETURN"
LINE 1: RETURN QUERY SELECT * FROM omnipay_lookups.countries WHERE o...
        ^
QUERY:  RETURN QUERY SELECT * FROM omnipay_lookups.countries WHERE omnipay_lookups.countries.country_name >= r
CONTEXT:  PL/pgSQL function "report_get_countries_new" line 14 at EXECUTE statement

I have tried other ways instead of this:

RETURN QUERY EXECUTE sql;

Way 1:

RETURN EXECUTE sql;

Way 2:

sql = 'RETURN QUERY SELECT * FROM....
/*later*/
EXECUTE sql;

In all cases without success.

Ultimately I want to write a stored procedure that contains a dynamic sql statement and that returns the result set from the dynamic sql statement.

  • 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-09T23:13:12+00:00Added an answer on June 9, 2026 at 11:13 pm

    There is room for improvements:

    CREATE OR REPLACE FUNCTION report_get_countries_new (starts_with text
                                                       , ends_with   text = NULL)
      RETURNS SETOF lookups.countries AS
    $func$
    DECLARE
       sql text := 'SELECT * FROM lookups.countries WHERE country_name >= $1';
    BEGIN
       IF ends_with IS NOT NULL THEN
          sql := sql || ' AND country_name <= $2';
       END IF;
    
       RETURN QUERY EXECUTE sql
       USING starts_with, ends_with;
    END
    $func$ LANGUAGE plpgsql;
    -- the rest is default settings
    

    Major points

    • PostgreSQL 8.4 introduced the USING clause for EXECUTE, which is useful for several reasons. Recap in the manual:

      The command string can use parameter values, which are referenced in
      the command as $1, $2, etc. These symbols refer to values supplied in
      the USING clause. This method is often preferable to inserting data
      values into the command string as text: it avoids run-time overhead of
      converting the values to text and back, and it is much less prone to
      SQL-injection attacks since there is no need for quoting or escaping.

      IOW, it is safer and faster than building a query string with text representation of parameters, even when sanitized with quote_literal().
      Note that $1, $2 in the query string refer to the supplied values in the USING clause, not to the function parameters.

    • While you return SELECT * FROM lookups.countries, you can simplify the RETURN declaration like demonstrated:

      RETURNS SETOF lookups.countries
      

      In PostgreSQL there is a composite type defined for every table automatically. Use it. The effect is that the function depends on the type and you get an error message if you try to alter the table. Drop & recreate the function in such a case.

      This may or may not be desirable – generally it is! You want to be made aware of side effects if you alter tables. The way you have it, your function would break silently and raise an exception on it’s next call.

    • If you provide an explicit default for the second parameter in the declaration like demonstrated, you can (but don’t have to) simplify the call in case you don’t want to set an upper bound with ends_with.

      SELECT * FROM report_get_countries_new('Zaire');
      

      instead of:

      SELECT * FROM report_get_countries_new('Zaire', NULL);
      

      Be aware of function overloading in this context.

    • Don’t quote the language name 'plpgsql' even if that’s tolerated (for now). It’s an identifier.

    • You can assign a variable at declaration time. Saves an extra step.

    • Parameters are named in the header. Drop the nonsensical lines:

       starts_with ALIAS FOR $1;
       ends_with ALIAS FOR $2;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write a neat PL/SQL stored procedure that can execute against a
Question How do I write a T-SQL Stored Procedure that lets me select percentages
I have a stored procedure that returns a list. Now I'm trying to write
We have an stored procedure that we created so that user can write comma
We have an stored procedure that we created so that user can write comma
I'm currently trying to write a stored procedure that can compute the biweekly periods
I am attempting to write a stored procedure that can take in k words
I am trying to write a stored procedure that will allow me to write
I'm trying to write a stored procedure that will have 6 bit value flags
Is it possible to write a stored procedure or trigger that will be executed

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.