String sum and receiving multiple row result query in comma separated single row.
Execution result of the query in Postgresql Function
CREATE TABLE mem_reg_mcg
(
id serial NOT NULL,
br_id character varying(3) NOT NULL,
mid character varying(50) NOT NULL,
"name" character varying(200)
);
CREATE OR REPLACE FUNCTION get_all_single_row_mem(m_id integer)Execution result of the query in Postgresql Function
CREATE TABLE mem_reg_mcg
(
id serial NOT NULL,
br_id character varying(3) NOT NULL,
mid character varying(50) NOT NULL,
"name" character varying(200)
);
RETURNS character varying AS
$BODY$
DECLARE
_record RECORD;
alert_mesg character varying(2000);
IN_VAR1 character varying(50);
BEGIN
alert_mesg := '';
--Define output columns
FOR _record IN select m.name from mem_reg_mcg m where id> $1
LOOP
IN_VAR1 := _record.name;
--Build output string
alert_mesg := alert_mesg||IN_VAR1||', ';
END LOOP;
RETURN subString(alert_mesg,0,char_length(alert_mesg)-1);
--EXCEPTION -- WHEN OTHERS THEN -- RETURN 'No troubleshooting information at this time.';
END;
$BODY$ LANGUAGE 'plpgsql' VOLATILE
COST 100;
ALTER FUNCTION get_all_single_row_mem(m_id integer) OWNER TO postgres;
See Cursor example
CREATE OR REPLACE FUNCTION MYCURSOR (VARINA VARCHAR) RETURNS VARCHAR AS $$
declare
--cur1 cursor is select A1, A2 from A;
cur1 refcursor;
cid integer;
_A1 varchar (10) ;
_A2 varchar (10) ;
alert_mesg VARCHAR(2000) := '';
BEGIN
--open cur1;
OPEN cur1 FOR execute('select * from A');
loop
fetch cur1 into _A1, _A2;
if not found then
exit ;
end if;
alert_mesg := alert_mesg||rpad(_A1,20)||rpad(_A2,20);
end loop;
close cur1;
return alert_mesg;
END;
$$ LANGUAGE plpgsql
Supporting URL help from Postgresql
http://archives.postgresql.org/pgsql-sql/2005-04/msg00052.php
No comments:
Post a Comment