VK
asked on
RETURN table columns issue
Hi,
we are getting syntax error when we have a return table column matches with any other table columns in the function. If we need to use of one of return table column data and insert into another table which has same column name, it seems, it is not allowed, any work around on this? please find below code.
we are getting syntax error when we have a return table column matches with any other table columns in the function. If we need to use of one of return table column data and insert into another table which has same column name, it seems, it is not allowed, any work around on this? please find below code.
CREATE OR REPLACE FUNCTION public.get_country9()
RETURNS TABLE (
[b]country_id[/b] integer,
country character varying(50),
last_update timestamp without time zone
)
AS $BODY$
BEGIN
RETURN QUERY
SELECT
c.country_id,
c.country,
c.last_update
FROM
public.country c;
insert into public.test_insert
( city,
[b]country_id[/b])
select 'LONDON',222;
insert into public.test_insert
( city,
[b]country_id[/b])
select 'TORONTO',111;
END;
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY INVOKER;
ERROR:
ERROR: 42601: syntax error at or near "$1"
ASKER
Thanks for the reply. Our requirement is along with returning return query output through table return also need to log part of the information in the log table which has some of column names same as return table columns. please find below test code for the scenario. please advise if you have any work around solution.
drop function if exists public.get_country9(intege r,characte r varying);
drop function if exists public.get_country9(intege
CREATE OR REPLACE FUNCTION public.get_country9(integer,character varying)
RETURNS TABLE (
[b]country_id[/b] integer,
country character varying(50),
last_update timestamp without time zone
)
AS $BODY$
DECLARE
v_country_id ALIAS FOR $1;
v_country ALIAS FOR $2;
BEGIN
insert into public.test_log
( [b]country_id[/b],
country)
select
v_country_id,
v_country;
RETURN QUERY
SELECT
c.[b]country_id[/b],
c.country,
c.last_update
FROM
public.country c
where c.country_id= v_country_id
or c.country=v_country;
END;
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY INVOKER;
hmm, I don't have a test instance. But you either use a stored procedure or you look into the audit capabilities of Postgres (e.g. pgAudit).
ASKER
we are using Postgres 8.23 on Greenplum, PgAudit is not available.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Did you mean something like this:
Open in new window