PostgreSQL 存储过程数据参数

发布于 2024-11-06 22:44:33 字数 519 浏览 0 评论 0原文

我有以下存储过程,它返回 0 个结果,但如果单独运行查询,则会产生大量结果。我缺少什么。

CREATE OR REPLACE FUNCTION countStatistics(baselineDate Date) RETURNS int AS $$
DECLARE
    qty int;
BEGIN
    SELECT COUNT(*) INTO qty FROM statistics WHERE time_stamp = baselineDate;
    RETURN qty;
END;
$$ LANGUAGE plpgsql;

--Execute the function
SELECT countStatistics('2015-01-01 01:00:00') as qty;

返回 0 个结果

SELECT COUNT(*) FROM statistics WHERE time_stamp = '2015-01-01 01:00:00';

返回 100+ 个结果

I have the following stored procedure, which returns 0 results but if the run the query by itself it result lot of results. What am i missing.

CREATE OR REPLACE FUNCTION countStatistics(baselineDate Date) RETURNS int AS $
DECLARE
    qty int;
BEGIN
    SELECT COUNT(*) INTO qty FROM statistics WHERE time_stamp = baselineDate;
    RETURN qty;
END;
$ LANGUAGE plpgsql;

--Execute the function
SELECT countStatistics('2015-01-01 01:00:00') as qty;

return 0 results

SELECT COUNT(*) FROM statistics WHERE time_stamp = '2015-01-01 01:00:00';

return 100+ results

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

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

发布评论

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

评论(1

迷爱 2024-11-13 22:44:33

您将 baselineDate 参数声明为 date

CREATE OR REPLACE FUNCTION countStatistics(baselineDate Date)

但将其用作 timestamp

SELECT COUNT(*) INTO qty FROM statistics WHERE time_stamp = baselineDate;

您将获得隐式转换,因此 countStatistics('2015-01-01 01:00:00') 将实际执行此 SQL:

SELECT COUNT(*) INTO qty FROM statistics WHERE time_stamp = '2015-01-01';

并且在 date 隐式转换回timestamp,它实际上是这样的:

SELECT COUNT(*) INTO qty FROM statistics WHERE time_stamp = '2015-01-01 00:00:00';

尝试更改函数声明以使用timestamp

CREATE OR REPLACE FUNCTION countStatistics(baselineDate timestamp)

You're declaring your baselineDate parameter as a date:

CREATE OR REPLACE FUNCTION countStatistics(baselineDate Date)

but using it as a timestamp:

SELECT COUNT(*) INTO qty FROM statistics WHERE time_stamp = baselineDate;

You're getting an implicit cast so countStatistics('2015-01-01 01:00:00') will actually execute this SQL:

SELECT COUNT(*) INTO qty FROM statistics WHERE time_stamp = '2015-01-01';

and, after the date is implicitly cast back to a timestamp, it will effectively be this:

SELECT COUNT(*) INTO qty FROM statistics WHERE time_stamp = '2015-01-01 00:00:00';

Try changing your function declaration to use a timestamp:

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