这是我正在运行的代码:
PREPARE result_have AS SELECT select_some_data(12345,'test_string'); PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'); SELECT results_eq('result_have','result_want');
这是失败输出:
not ok 21 # Failed test 21: "this should return a result" # Columns differ between queries: # have: ("(""2010-09-07 06:05:00+00"",100.0)") # want: ("(""2010-09-07 06:05:00+00"",100.0)") # Looks like you Failed 1 test of 21
我可能真的睡不着觉,但想要和我看起来很相似.
有谁知道为什么这被报告为失败?
有关详细信息的更新:这是我如何定义有问题的存储过程:
CREATE OR REPLACE FUNCTION select_some_data ( IN p_some_pkey integer,IN p_some_code varchar(16) ) RETURNS TABLE(timestamp_utc timestamp with time zone,value varchar) ...
所以要遵循peters advice,我尝试更改我的代码,但没有成功:
PREPARE result_have AS SELECT select_some_data(12345,'test_string'); -- Todo: none of these work,Syntax error at or near "TABLE" -- PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::TABLE(timestamp with time zone,varchar)); -- PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::'TABLE(timestamp with time zone,varchar)'); -- this is the old code... PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'); SELECT results_eq('result_have','result_want');
正如你可能会说的那样,即使是基本的postgresql语法,我也很难在黑暗中刺伤 – 谷歌和postgresql.org上的搜索都没有在搜索::时返回任何有用的内容.我最终冒了一个幸运的猜测,这可能是一个运算符,并发现::是type cast. CREATE FUNCTION的column_name参数文档说’RETURNS TABLE也暗示RETURNS SetoF’让我得到例如新的尝试:here,也许是here和here.
PREPARE result_have AS SELECT select_some_data(12345,'test_string'); -- Todo: doesn't work,Syntax error at or near "(" -- PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::SetoF(timestamp with time zone,varchar)); -- Todo: doesn't work,Syntax error at or near "," -- PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::SetoF RECORD(timestamp with time zone,varchar)); -- this is the old code... PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",'result_want');
这是毫无意义的,我只是在做猜测.任何人都可以用正确的语法帮助我吗?请注意,该函数只是RETURNS TABLE,因为这是我能够开始工作的第一件事,所以如果有一个解决方案需要改变它,我很乐意改变它.
更新2:postgresql IRC频道上的RhodiumToad(irc://irc.freenode.net/#postgresql)帮助我使用了正确的语法.如果我对postgresql了解得多一些,我可以认为这只是有意义的:两种数据类型,两个演员阵容(DOH!):o).
此时,测试数据库中只有一个数据集,因此上面使用的语法可能仍然有效.据我所知,一旦返回多个数据集,它可能会失败,因此它应该是SELECT * FROM,而不仅仅是SELECT:
PREPARE result_have AS SELECT * FROM select_some_data(12345,'test_param_code'); PREPARE result_want AS VALUES ('2010-09-07 06:05:00+00'::timestamp with time zone,'100.0'::varchar); SELECT results_eq('result_have','result_want','have and want should be equal');
现在已经和希望结果相等,测试通过.运行测试时的日志输出:
ok 21 - have and want should be equal ok All tests successful. Files=1,Tests=21,1 wallclock secs ( 0.02 usr 0.00 sys + 0.05 cusr 0.03 csys = 0.10 cpu) Result: PASS
WOOT!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。