动态查询postgres [英] dynamic query postgres
问题描述
我是新的postgres并运行以下动态查询
I am new to postgres and running following dynamic query
EXECUTE 'Select * from products';
我收到以下回应。
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
推荐答案
有 plpgsql 的 EXECUTE
语句,这将做你想要的do - 执行一个SQL查询字符串。您标记了的问题,因此这可能是您所在的寻找。
There is the EXECUTE
statement of plpgsql, which would do what you are trying to do - execute an SQL query string. You tagged dynamic, so this may be what you are looking for.
只能在plpgsql函数内或 DO
语句(匿名代码块)。 EXECUTE
和 SQL-EXECUTE
在精细手册中明确:
Only works inside plpgsql functions or DO
statements (anonymous code blocks). The distinction between EXECUTE
and SQL-EXECUTE
made clear in the fine manual:
注意: PL / pgSQL
EXECUTE
语句与EXECUTE
PostgreSQL服务器支持的SQL
语句。服务器的EXECUTE
语句不能直接在PL / pgSQL函数中使用(而且不需要
)。
Note: The PL/pgSQL
EXECUTE
statement is not related to theEXECUTE
SQL statement supported by the PostgreSQL server. The server'sEXECUTE
statement cannot be used directly within PL/pgSQL functions (and is not needed).
如果要从动态 SELECT
查询返回值,如您所示,您需要创建一个函数。 DO
语句总是返回 void
。 有关从函数中返回值的更多信息非常精细的手册。
If you want to return values from a dynamic SELECT
query as your example indicates, you need to create a function. DO
statements always return void
. More about returning values from a function in the very fine manual.
这篇关于动态查询postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!