我有这样的功能:
CREATE OR REPLACE FUNCTION get_path_set_1(IN pathset_id_in character varying, OUT id character varying, OUT pathset_id character varying, OUT utility double precision)
RETURNS SETOF record AS
$BODY$
begin
if exists(SELECT 1 FROM "PathSet_Scaled_HITS_distinctODs" WHERE "ID" = $1) then
return query SELECT "ID", "PATHSET_ID", "UTILITY"
FROM "SinglePath_Scaled_HITS_distinctODs"
where "PATHSET_ID" = $1;
end if;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION get_path_set_1(character varying)
OWNER TO postgres;
当我在我的程序中使用它来调用它时:
std::string testStr("43046,75502");// or std::string testStr("'43046,75502'");
soci::rowset<sim_mob::SinglePath> rs = (sql.prepare << "get_path_set_1(:pathset_id_in)",soci::use(testStr));
我得到以下异常:
terminate called after throwing an instance of 'soci::postgresql_soci_error'
what(): Cannot prepare statement. ERROR: syntax error at or near "get_path_set_1"
LINE 1: get_path_set_1($1)
如果你帮助我发现缺失部分,我将不胜感激 谢谢
答案 0 :(得分:1)
不解决您报告的错误。但是要简化你的功能:
CREATE OR REPLACE FUNCTION get_path_set_1(pathset_id_in varchar)
RETURNS TABLE(id varchar, pathset_id varchar, utility double precision) AS
$func$
BEGIN
RETURN QUERY
SELECT "ID", "PATHSET_ID", "UTILITY"
FROM "SinglePath_Scaled_HITS_distinctODs"
WHERE "PATHSET_ID" = $1;
END
$func$ LANGUAGE plpgsql;
RETURNS TABLE
是组合RETURNS SETOF record
和OUT
参数的现代,更优雅,等效的形式。
IF exists ...
在这里买不到任何东西。运行查询;如果没有找到,则不返回任何内容。相同的结果是成本的一半。
答案 1 :(得分:0)
从这段代码:
soci::rowset<sim_mob::SinglePath> rs =
(sql.prepare << "get_path_set_1(:pathset_id_in)",soci::use(testStr));
您似乎正在尝试准备一个只包含函数调用的查询,甚至没有SELECT。
这在SQL中无效。您想要准备此查询:
SELECT * FROM get_path_set_1(:pathset_id_in)
此表单(select * from function(...)
)也是必需的,因为该函数返回的结果集包含多个列,而不是标量值。
同样在Erwin提及的情况下,OUT
和SETOF RECORD
在这种情况下很奇怪,我会在使用RETURNS TABLE
时提出建议。