How building a dynamic query in a postgresql-function?

Posted on 2005-05-16
Last Modified: 2009-12-16

I would like to know how I've to build and execute a dynamic sql query in a postgresql-function.

More details :

I've 3 parameters :
  * Customer Name
  * Customer First Name
  * Customer Account Number

some of those fields are filled in. The postgre-sql function receive the 3 parameters. In the function I want to create a select query with the filled-in values (not blank values).

I've noticed that the execute functions doesn't return any results.

Can you please send me a short example?

Thanks a lot!

Question by:davyberroho
    LVL 22

    Accepted Solution

    In what context are you doing this -- the only rational context is pl/pgsql
    I which case you would use an explicit cursor viz

    OPEN curs1 FOR EXECUTE 'SELECT * FROM ' || quote_ident($1);

    or implicit cursor viz

    FOR record_or_row IN EXECUTE text_expression LOOP


    Author Comment

    thanks, exactly what I needed.

    works great !

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: ( This requires some add-o…
    Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
    Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now