postgres de consulta dinámica

I am new to postgres and running following dynamic query

EXECUTE 'Select * from products';

I get following response.

ERROR: syntax error at or near "'Select * from products'" 
LINE 1: EXECUTE 'Select * from products';

I Know this would be something basic I m missing

preguntado el 22 de mayo de 12 a las 21:05

2 Respuestas

Aquí debe visitar el EXECUTE declaracion de plpgsql, which would do what you are trying to do - execute an SQL query string. You tagged , so this may be what you are looking for.

Only works inside plpgsql functions or DO statements (anonymous code blocks). La distinción entre EXECUTE y SQL-EXECUTE made clear in the fine manual:

Nota: The PL/pgSQL EXECUTE statement is not related to the EXECUTE SQL statement supported by the PostgreSQL server. The server's EXECUTE statement cannot be used directly within PL/pgSQL functions (and is not needed).

If you want to return values from a dynamic SELECT query as your example indicates, you need to create a function. DO las declaraciones siempre regresan void. More about returning values from a function in the very fine manual.

contestado el 22 de mayo de 12 a las 22:05

Desde el fino manual:


EXECUTE name [ ( parameter [, ...] ) ]


EXECUTE is used to execute a previously prepared statement.

So EXECUTE doesn't execute a string of SQL, it execute a prepared statement that is identified by a name and you need to prepare the statement separately using PREPARAR:

=> prepare stmt as select * from products;
=> execute stmt;
-- "select * from products" output goes here...

contestado el 22 de mayo de 12 a las 21:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.