简体   繁体   English

SOCI无法准备声明

[英]SOCI Cannot prepare statement

I have a function like this: 我有一个这样的功能:

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;

when I call it in my program using this: 当我使用以下代码在程序中调用它时:

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));

I get the following exception: 我得到以下异常:

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)

I will appreciate if you help me detect missing part thank you 如果您能帮助我检测到缺失的部分,我将不胜感激,谢谢

This does not solve the error you report. 解决您报告错误。 But simplify your function: 但是简化您的功能:

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 is the modern, more elegant, equivalent form of the combination RETURNS SETOF record and OUT parameters. RETURNS TABLERETURNS SETOF recordOUT参数组合的一种现代,更优雅的等效形式。

  • IF exists ... is buying you nothing here. IF exists ...在这里什么也没买。 Run the query; 运行查询; if nothing is found, nothing is returned. 如果未找到任何内容,则不会返回任何内容。 Same result for half the cost. 同样的结果只需一半的成本。

From this piece of code: 从这段代码中:

soci::rowset<sim_mob::SinglePath> rs =
  (sql.prepare << "get_path_set_1(:pathset_id_in)",soci::use(testStr));

it appears you're trying to prepare a query that just contains the function call without even a SELECT. 看来您正在尝试准备一个仅包含函数调用而没有SELECT的查询。

That's not valid in SQL. 在SQL中无效。 You want to prepare this query instead: 您想改为准备此查询:

 SELECT * FROM get_path_set_1(:pathset_id_in)

This form ( select * from function(...) ) is also necessary because the function returns a resultset with multiple columns, as opposed to just a scalar value. 这种形式( select * from function(...) )也是必需的,因为该函数返回一个具有多列的结果集,而不仅仅是标量值。

Also as Erwin mentions, the OUT and SETOF RECORD are weird in this case, I'll second his advice on using RETURNS TABLE . 同样正如Erwin所提到的,在这种情况下, OUTSETOF RECORD很奇怪,我RETURNS TABLE使用RETURNS TABLE提出他的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM