Reputation: 73
Please help me to resolve this error.
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
When i try to execute this query it is showing me the error as
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
Upvotes: 3
Views: 2551
Reputation: 73
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
Upvotes: 2
Reputation: 117636
I think this is happening because cursor returning record type, not hstore. You can change your function like:
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;
Upvotes: 3