我一直在网上寻找这个答案,但我找不到它.
我试图通过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;
最佳答案 试试这个:
CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord dim_date) RETURNS void AS $$
dim_date必须是一个表.
编辑:
好的,现在我真的很困惑.
>日期应该是列,而不是表.我不明白为什么要创建一个包含日期值的表.
>您可以格式化日期没有问题to_char.阅读本文:Data Type Formatting Functions学习如何操作.你创建的那个功能没有任何意义.
>你在输出PL / pgSQL吗?格式化不应该由中间层完成吗?您应该只从数据库返回一个日期.
最后,我建议阅读PL / pgSQL Manual.那里有很多好东西.