How building a dynamic query in a postgresql-function?

Hello,

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!


Davy.
davyberrohoAsked:
Who is Participating?
 
earth man2Connect With a Mentor Commented:
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
    statements
END LOOP;


0
 
davyberrohoAuthor Commented:
thanks, exactly what I needed.

works great !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.