如何将记录作为参数传递给 PL/pgSQL 函数?

发布于 2024-09-26 14:53:32 字数 1716 浏览 8 评论 0原文

我一直在网上寻找这个答案,但找不到。

我正在尝试通过 PL/pgSQL 函数传递一条记录。我尝试了两种方法。

拳头方式

CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord dim_date%ROWTYPE) RETURNS void AS $$

这就是输出:

psql:requestExample.sql:21: ERROR:  syntax error at or near "%"
LINE 1: ... FUNCTION translateToReadableDate(mRecord dim_date%ROWTYPE) ...
                                                             ^

第二种方式

CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord RECORD) RETURNS void AS $$

还有输出

psql:requestExample.sql:21: ERROR:  PL/pgSQL functions cannot accept type record

有人知道怎么做吗?

CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord dim_date) RETURNS void AS $$

    BEGIN

    SELECT dim_day.name || ' (' || dim_day_in_month.id || ') ' || dim_month.name   || 'is the ' || dim_week.id || ' week of the year. ' AS "Une phrase", dim_quarter.id, dim_year.id
    FROM dim_date dd
     JOIN dim_day ON dd.day_id = dim_day.day_id
     JOIN dim_day_in_month ON dd.day_in_month_id = day_in_month.day_in_month_id
     JOIN dim_week ON dd.week_id = dim_week.week_id
     JOIN dim_month ON dd.month_id = dim_month.month_id
     JOIN dim_quarter ON dd.quarter_id = dim_quarter.quarter_id
     JOIN dim_year ON dd.year_id = dim_year.year_id
    WHERE dd.day_id = mRecord.day_id
     AND dd.day_in_month_id = mRecord.day_in_month_id
     AND dd.week_id = mRecord.week_id
     AND dd.month_id = mRecord.month_id
     AND dd.quarter_id = mRecord.quarter_id
     AND dd.year_id = mRecord.year_id;

    END;
    $$ LANGUAGE plpgsql;

I keep looking for this answer online but I cannot find it.

I am trying to pass one record over a PL/pgSQL function. I tried it in two ways.

Fist way :

CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord dim_date%ROWTYPE) RETURNS void AS $

That is the ouput :

psql:requestExample.sql:21: ERROR:  syntax error at or near "%"
LINE 1: ... FUNCTION translateToReadableDate(mRecord dim_date%ROWTYPE) ...
                                                             ^

Second way :

CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord RECORD) RETURNS void AS $

And there is the output

psql:requestExample.sql:21: ERROR:  PL/pgSQL functions cannot accept type record

Someone does know how to do this please ?

CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord dim_date) RETURNS void AS $

    BEGIN

    SELECT dim_day.name || ' (' || dim_day_in_month.id || ') ' || dim_month.name   || 'is the ' || dim_week.id || ' week of the year. ' AS "Une phrase", dim_quarter.id, dim_year.id
    FROM dim_date dd
     JOIN dim_day ON dd.day_id = dim_day.day_id
     JOIN dim_day_in_month ON dd.day_in_month_id = day_in_month.day_in_month_id
     JOIN dim_week ON dd.week_id = dim_week.week_id
     JOIN dim_month ON dd.month_id = dim_month.month_id
     JOIN dim_quarter ON dd.quarter_id = dim_quarter.quarter_id
     JOIN dim_year ON dd.year_id = dim_year.year_id
    WHERE dd.day_id = mRecord.day_id
     AND dd.day_in_month_id = mRecord.day_in_month_id
     AND dd.week_id = mRecord.week_id
     AND dd.month_id = mRecord.month_id
     AND dd.quarter_id = mRecord.quarter_id
     AND dd.year_id = mRecord.year_id;

    END;
    $ LANGUAGE plpgsql;

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

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

发布评论

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

评论(1

甩你一脸翔 2024-10-03 14:53:32

试试这个:

CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord dim_date) RETURNS void AS $

dim_date 必须是一个表。

编辑:

好的,现在我真的很困惑。

  1. 日期应该是一列,而不是表格。我不明白为什么要创建一个包含日期值的表。
  2. 使用 to_char 可以毫无问题地格式化日期。请阅读以下内容:数据类型格式化函数以了解如何操作。您创建的那个函数毫无意义。
  3. 你输出PL/pgSQL吗?格式化不应该由中间层完成吗?您应该只从数据库返回一个日期。

最后,我建议阅读 PL/pgSQL 手册。里面有很多好东西。

Try this:

CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord dim_date) RETURNS void AS $

dim_date must be a table.

EDIT:

Ok, now I'm really really confused.

  1. A date should be a column, not a table. I can't understand why would you create a table with date values.
  2. You can format dates no problem with to_char. Read this: Data Type Formatting Functions to learn how to. That function you created makes zero sense.
  3. Are you outputting PL/pgSQL? Shouldn't the formatting be done by the middle tier? You should just return a Date from the database.

Lastly, I would recommend reading the PL/pgSQL Manual. There's lots of good stuff in there.

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