动态查询postgres [英] dynamic query postgres

查看:182
本文介绍了动态查询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 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).

如果要从动态 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆