PL/PgSQL:RETURNS TABLE 输出以逗号分隔,没有值

发布于 2024-12-03 20:40:53 字数 599 浏览 0 评论 0 原文

以下 PL/pgSQl 脚本返回正确的行数,但输出是括号中的逗号分隔值列表,如下所示:

(,,) (,,) (,,) (,,) (,,) 。 。 (,,)

CREATE OR REPLACE FUNCTION Get_Airports_in_Country(countryCode TEXT) 
RETURNS TABLE(gid int, iko text, name text) AS $$
 DECLARE
    cntry_geom cntry02.the_geom%TYPE;       
 BEGIN   
    SELECT the_geom INTO cntry_geom from cntry02 where iso_2digit = $1;

    RETURN QUERY
    SELECT gid, iko, name
      FROM airport
     WHERE ST_Within(the_geom, cntry_geom);
 END;
$$
LANGUAGE plpgsql;

SELECT Get_Airports_in_Country('CA');

我使用的是 PostgreSQL 8.4。

知道我在这里缺少什么吗?

The following PL/pgSQl script returns the correct number of rows, but the output is a list of comma separated values in brackets as follows:

(,,) (,,) (,,) (,,) (,,) . . (,,)

CREATE OR REPLACE FUNCTION Get_Airports_in_Country(countryCode TEXT) 
RETURNS TABLE(gid int, iko text, name text) AS $
 DECLARE
    cntry_geom cntry02.the_geom%TYPE;       
 BEGIN   
    SELECT the_geom INTO cntry_geom from cntry02 where iso_2digit = $1;

    RETURN QUERY
    SELECT gid, iko, name
      FROM airport
     WHERE ST_Within(the_geom, cntry_geom);
 END;
$
LANGUAGE plpgsql;

SELECT Get_Airports_in_Country('CA');

I am using PostgreSQL 8.4.

Any idea what I am missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-12-10 20:40:53

它可能看起来像这样:

CREATE OR REPLACE FUNCTION get_airports_in_country(text)
RETURNS TABLE(gid int, iko text, name text) AS $x$
DECLARE
-- $1 .. countryCode text   -- just one way to comment
   cntry_geom cntry02.the_geom%TYPE;       
BEGIN

cntry_geom := c.the_geom FROM cntry02 c WHERE c.iso_2digit = $1;

RETURN QUERY
SELECT a.gid, a.iko, a.name
  FROM airport a
 WHERE ST_Within(a.the_geom, cntry_geom);

END;
$x$
LANGUAGE plpgsql;

调用:

SELECT * FROM get_airports_in_country('CA');

要点: 无论如何,

  • 为您要通过数字引用的 IN 参数定义名称并没有多大用处。导致不必要的命名冲突。在这种情况下,请添加评论。
  • 命名冲突是您的核心问题。当您使用名称 gidikonamereuse 作为 OUT 参数时(在 RETURNS TABLE..),您必须在函数体中限定相同的列名称。 -> a.gid、a.iko、a.name。 PostgreSQL 9.1 对这些东西更加严格并且会抛出错误。本来可以帮助你,但你运行在 8.4 上。
  • 只要不使用双引号,在 postgres 中使用混合大小写标识符就没有意义。 (不过,这并没有错。)
  • 这称为函数存储过程。与“PL/pgSQl 脚本”最接近的东西是 DO 语句,在 PostgreSQL 9.0 中引入。

It might look like this:

CREATE OR REPLACE FUNCTION get_airports_in_country(text)
RETURNS TABLE(gid int, iko text, name text) AS $x$
DECLARE
-- $1 .. countryCode text   -- just one way to comment
   cntry_geom cntry02.the_geom%TYPE;       
BEGIN

cntry_geom := c.the_geom FROM cntry02 c WHERE c.iso_2digit = $1;

RETURN QUERY
SELECT a.gid, a.iko, a.name
  FROM airport a
 WHERE ST_Within(a.the_geom, cntry_geom);

END;
$x$
LANGUAGE plpgsql;

Call:

SELECT * FROM get_airports_in_country('CA');

Major points:

  • There is not much use in defining names for IN parameters that you are going to reference by number anyway. Leads to unnecessary naming conflicts. Add a comment instead, in such a case.
  • A naming conflict is your core problem. As you use the names gid, iko, namereuse as OUT parameters (in the RETURNS TABLE..), you have to qualify the identical column names in the function body. -> a.gid, a.iko, a.name. PostgreSQL 9.1 is stricter about that stuff and throws errors. Would have helped you, but you run on 8.4.
  • No point in using mixed case identifiers in postgres, as long as you don't double quote them. (It's not wrong, though.)
  • This is called a function or stored procedure. The closest thing to a "PL/pgSQl script" would be the "anonymous code block" of a DO statement, introduced in PostgreSQL 9.0.
不忘初心 2024-12-10 20:40:53

你应该使用不同的查询

SELECT * FROM Get_Airports_in_Country('CA');

you should to use a different query

SELECT * FROM Get_Airports_in_Country('CA');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文