postgres游标中的错误

时间:2013-09-23 05:43:24

标签: postgresql plpgsql

请帮我解决此错误。

CREATE OR REPLACE FUNCTION MYCURSOR () RETURNS VARCHAR AS $$
declare

cur1 refcursor;

col_name varchar (10) ;
hstoredata hstore;
BEGIN

col_name = 'id';
OPEN cur1 FOR execute('select * from datas.tb where id =2');
loop
 fetch cur1 into hstoredata;
 if not found then
 exit ;
 end if;
    Raise Notice '%',hstoredata -> col_name ;

 end loop;
 close cur1;
 return 'r';
 END;
 $$ LANGUAGE plpgsql

当我尝试执行此查询时,它显示错误为

ERROR:  Unexpected end of string
CONTEXT:  PL/pgSQL function "mycursor" line 15 at FETCH

********** Error **********

ERROR: Unexpected end of string
SQL state: XX000
Context: PL/pgSQL function "mycursor" line 15 at FETCH

2 个答案:

答案 0 :(得分:3)

我认为这种情况正在发生,因为游标返回记录类型,而不是hstore。您可以更改以下功能:

CREATE OR REPLACE FUNCTION MYCURSOR()
RETURNS VARCHAR AS 
$$
declare
    cur1 refcursor;
    col_name varchar(10);
    rec record;
begin
    col_name := 'id';
    open cur1 for execute('select 1 as id');
    loop
       if not found then
           exit ;
       end if;
       fetch cur1 into rec;
       Raise Notice '%', rec.<column with hstore>-> col_name;
    end loop;
    close cur1;
    return 'r';
end;
$$ LANGUAGE plpgsql;

答案 1 :(得分:2)

 CREATE OR REPLACE FUNCTION MYCURSOR () RETURNS VARCHAR AS $$
  declare

    cur1 refcursor;
    var1 varchar (10) ;
    hstoredata hstore;
    r record;
    alert_mesg VARCHAR(2000) := '';
       BEGIN

         var1 = 'id';
         OPEN cur1 FOR execute('select * from datas.tb where id =2');
         loop
            fetch cur1 into r;
            if not found then
                 exit ;
            end if;
        select hstore(r) into hstoredata;
            Raise Notice '%',hstoredata->'id';

         end loop;
         close cur1;
         return alert_mesg;
    END;
   $$ LANGUAGE plpgsql