PostgreSQL 存储过程数据参数
我有以下存储过程,它返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
baselineDate
参数声明为date
:但将其用作
timestamp
:您将获得隐式转换,因此
countStatistics('2015-01-01 01:00:00')
将实际执行此 SQL:并且在
date
隐式转换回timestamp
,它实际上是这样的:尝试更改函数声明以使用
timestamp
:You're declaring your
baselineDate
parameter as adate
:but using it as a
timestamp
:You're getting an implicit cast so
countStatistics('2015-01-01 01:00:00')
will actually execute this SQL:and, after the
date
is implicitly cast back to atimestamp
, it will effectively be this:Try changing your function declaration to use a
timestamp
: