I have a storage procedure as shown below
CREATE FUNCTION select_transactions3(text, text, int) RETURNS SETOF transactions AS $body$ DECLARE rec transactions%ROWTYPE; BEGIN FOR rec IN (SELECT invoice_no, trans_date FROM transactions WHERE $1 = $2 limit $3 ) LOOP RETURN NEXT rec; END LOOP; END; $body$ LANGUAGE plpgsql VOLATILE SECURITY DEFINER;
when I execute the request as follows:
select * from select_transactions3("invoice_no", '1103300105472',10);
or
select * from select_transactions3(invoice_no, '1103300105472',10);
an error occurs: ERROR: column "invoice_no" does not exist
but when I try to execute with a single colon, like this:
select * from select_transactions3('invoice_no', '1103300105472',10);
as a result there is no line.
How can I get this data:
invoice_no | trans_date ---------------+------------------------- 1103300105472 | 2011-03-30 12:25:35.694
thanks.
UPDATE: if we need a specific column of the table we want to show
CREATE FUNCTION select_to_transactions14(_col character varying, _val character varying, _limit int) RETURNS SETOF RECORD AS $$ DECLARE rec record; BEGIN FOR rec IN EXECUTE 'SELECT invoice_no, amount FROM transactions WHERE ' || _col || ' = $1 LIMIT $2' USING _val, _limit LOOP RETURN NEXT rec; END LOOP; END; $$ LANGUAGE plpgsql;
to get the result:
SELECT * FROM select_to_transactions14( 'invoice_no', '1103300105472',1) as ("invoice_no" varchar(125), "amount" numeric(12,2));