Skip to content
Advertisement

How to run a string containing a static SQL query in PostgreSQL?

I have a function which generates a static SQL query and returns it as a string. What I need to do is to run the returned string as if I was typing it directly in psql. Here is a very simplified test case (the true mechanism is far more complicated but the following test case at least shows the idea)

Now if I run the following:

This gives me the sql query. But what I want to do is to run the query from the returned string as if I was writing manually select * from person in psql and hitting Enter in order to obtain the result of the query.

I’ve checked PREPARE, EXECUTE on the online documentation but so far I’ve not been able to achieve this. Could you kindly make some clarification?

Advertisement

Answer

And answer specific for psql is to change your semicolon to gexec.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement