Rob*_*ert 5 postgresql types rowtype
我正在尝试从PostgreSQL函数返回自定义类型,如下所示:
DROP TYPE IF EXISTS GaugeSummary_GetDateRangeForGauge_Type CASCADE; -- Drop our previous type
CREATE TYPE GaugeSummary_GetDateRangeForGauge_Type AS -- Recreate our type
(
Minimum timestamp without time zone,
Maximum timestamp without time zone
);
CREATE OR REPLACE FUNCTION GaugeSummary_GetDateRangeForGauge
(
GaugeID integer
)
RETURNS GaugeSummary_GetDateRangeForGauge_Type AS
$$
DECLARE
iGaugeID ALIAS FOR $1;
oResult GaugeSummary_GetDateRangeForGauge_Type%ROWTYPE;
BEGIN
SELECT INTO oResult
min(ArchivedMinimumTime) as Minimum,
max(TelemeteredMaximumTime) as Maximum
FROM GaugeSummary
WHERE GaugeID = $1;
RETURN oResult;
END;
$$ LANGUAGE plpgsql;
SELECT GaugeSummary_GetDateRangeForGauge(2291308);
Run Code Online (Sandbox Code Playgroud)
我遇到了两个问题.
1) - 我的结果作为单个列回复为"("1753-01-01 12:00:00","2009-11-11 03:45:00")",我需要他们回来两列.
解决了! - 愚蠢的错误......它应该是SELECT*FROM GaugeSummary_GetDateRangeForGauge(123)
2)结果是整个表中的最大值和最小值 - 未使用WHERE约束.
例:
GaugeSummaryID GaugeID ArchivedMinimumTime TelemeteredMaximumTime
80 4491 "2009-03-28 12:00:00" "2009-06-27 12:00:00"
81 4491 "2009-03-28 12:00:00" "2009-06-27 12:00:00"
Run Code Online (Sandbox Code Playgroud)
但是对函数的调用给了我:"1753-01-01 12:00:00","2009-11-11 03:45:00"
谢谢!
似乎在"LANGUAGE'SQL'STABLE;"中运行同一个查询 功能正常:
CREATE OR REPLACE FUNCTION GaugeSummary_GetDateRangeForGauge
(
GaugeID integer
)
RETURNS GaugeSummary_GetDateRangeForGauge_Type AS
$$
SELECT min(ArchivedMinimumTime) as Minimum,
max(TelemeteredMaximumTime) as Maximum
FROM GaugeSummary WHERE GaugeID = $1;
$$ LANGUAGE 'SQL' STABLE;
Run Code Online (Sandbox Code Playgroud)
但是,知道为什么plpgsql函数不能正常工作会很好....
我试过这个,我做了两个回来
SELECT * GaugeSummary_GetDateRangeForGauge(1);
Run Code Online (Sandbox Code Playgroud)
结果:
aadb=# select * from GaugeSummary_GetDateRangeForGauge(1);
minimum | maximum
----------------------------+----------------------------
2010-01-11 15:14:20.649786 | 2010-01-11 15:14:24.745783
(1 row)
Run Code Online (Sandbox Code Playgroud)
我使用8.4并在psql中运行它.你能澄清一下如何得到你的结果吗?
对于#2,如果您只想要结果,则从查询中删除min()和max()聚合函数.删除这些将确保将在与您的ID匹配的行上返回这些列的结果.
更新:好的我不知道那时候会发生什么.我只是将所有类似的东西放入我的测试数据库中,并按照我的预期运行.
create type custom_type as (
minimum timestamp without time zone,
maximum timestamp without time zone);
Run Code Online (Sandbox Code Playgroud)
aadb=# select * from test order by id;
id | a | b
----+----------------------------+----------------------------
1 | 2010-01-11 17:09:52.329779 | 2010-01-11 17:09:52.329779
1 | 2010-01-11 17:10:04.729776 | 2010-01-11 17:10:04.729776
2 | 2010-01-11 17:09:55.049781 | 2010-01-11 17:10:21.753781
2 | 2010-01-11 17:10:30.501781 | 2010-01-11 17:10:30.501781
3 | 2010-01-11 17:09:58.289772 | 2010-01-11 17:09:58.289772
3 | 2010-01-11 17:35:38.089853 | 2010-01-11 17:35:38.089853
(6 rows)
Run Code Online (Sandbox Code Playgroud)
create or replace function maxmin (pid integer) returns custom_type as $$
declare
oResult custom_type%rowtype;
begin
select into oResult min(a) as minimum, max(b) as maximum
from test where id = pid;
return oResult;
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)
aadb=# select * from maxmin(2);
minimum | maximum
----------------------------+----------------------------
2010-01-11 17:09:55.049781 | 2010-01-11 17:10:30.501781
(1 row)
Run Code Online (Sandbox Code Playgroud)