3

请帮我解决这个错误。

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

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

错误:字符串意外结束
上下文:PL/pgSQL 函数“mycursor”在 FETCH 的第 15 行

********** 错误 **********

错误:字符串意外结束
SQL状态:XX000
上下文: PL/pgSQL 函数“mycursor”在 FETCH 的第 15 行
4

2 回答 2

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;
于 2013-09-23T06:12:58.560 回答
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
于 2013-09-30T12:14:53.770 回答