Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a String Sql in PostgreSQL [duplicate]

Tags:

postgresql

In a function, I have a SELECT query in a string, for example:

sql='SELECT * FROM A'

I want to execute sql output result of: SELECT * FROM A

How can I execute the string sql in PostgreSQL?

like image 824
D T Avatar asked Jul 25 '13 10:07

D T


2 Answers

Inside a function use EXECUTE.
http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

like image 54
user2478690 Avatar answered Dec 11 '22 05:12

user2478690


below one works fine in postgres 8.4

UDBI=> PREPARE query as select 1 as a;
PREPARE
UDBI=> PREPARE query
UDBI=> EXECUTE query;
 a 
---
 1
(1 row)

UDBI=> 
like image 21
Vinit Prajapati Avatar answered Dec 11 '22 04:12

Vinit Prajapati