【问题标题】:plpgsql function which contains the query "Select * from table name"包含查询“Select * from table name”的 plpgsql 函数
【发布时间】:2015-10-24 03:55:26
【问题描述】:

我想创建一个 plpgsql 函数来执行一个简单的选择查询,即“从表名中选择 *”。当我将通过这个查询运行该函数时,就像 "select function()",则返回输出为"Select * from table name"。

【问题讨论】:

  • 你真的希望函数返回字符串值'Select * from table name'吗?还是您希望它返回该查询的 result
  • 其实我想要那个查询的结果。

标签: postgresql plpgsql


【解决方案1】:

不需要 PL/pgSQL 函数。一个简单的 SQL 函数就可以了:

create or replace function get_result()
  returns setof table_name
as
$$
  select * from table_name;
$$
language sql;

但是你需要使用select * from function();来得到结果不是select function()因为集合返回函数只能在from子句中使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 2019-06-26
    • 2022-12-07
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多