Column Reference Is Ambiguous in Postgresql Function

Why I am getting column reference *** is ambiguous ?

From the documentation,

36.4.9. SQL Functions Returning TABLE

There is another way to declare a function as returning a set, which
is to use the syntax RETURNS TABLE(columns). This is equivalent to
using one or more OUT parameters plus marking the function as
returning SETOF record (or SETOF a single output parameter's type, as
appropriate). This notation is specified in recent versions of the SQL
standard, and thus may be more portable than using SETOF.

That means when you declare the function with RETURNS TABLE(jobnr character varying..., jobnr is an out parameter. Thus SELECT jobnr ... is ambiguous.

Try declaring the function with aliases for the tables in your select:

CREATE OR REPLACE FUNCTION userdata.test3()
RETURNS TABLE(jobnr character varying, ordernr character varying)
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN QUERY
SELECT w.jobnr, w.ordernr
FROM
(SELECT wip.jobnr, wip.ordernr FROM userdata.WIP_Data as wip WHERE Year=2015 AND period=1) AS W

LEFT OUTER JOIN
(SELECT wip.jobnr, wip.ordernr FROM userdata.WIP_Data as wip WHERE Year=2015 AND period=2) AS P
ON W.Jobnr=P.Jobnr;
END;
$function$

How fix problem column reference id is ambiguous in PostgreSQL function?

The final line of the query should be

SELECT result.id, result.name,... FROM result

To avoid such collisions, you can use different names for the columns in the RETURNS TABLE clause (which are variables) and the columns in the queries (e.g. by using aliases).

Ambiguous column reference - it could refer to either a PL/pgSQL variable or a table column?

The conflict is between the variables in RETURNS TABLE and the field names returned from the query. Table qualify the field names in the query e.g. topic.topicid, topic.typid, topic.topic.

Why postgres trigger function returns some columns reference is ambiguous?

demo:db<>fiddle

You have to explicitly reference table_1_sum in the calculation:

sum_value = table_1_sum.sum_value + delta_value; 

What is this ERROR: ambiguous column reference error ?

The problem is here:

where id::varchar = code

because ate_history has a column called code and you defined a variable code, so it's ambiguous as to which one the expression refers to, as both are in scope.

Normally you would just must qualify it, but you can't so just rename it uniquely.

declare _code

_code := (select nullif(regexp_replace(r::varchar, '\D','','g'), ''));

where id::varchar = _code

or if you wanted the column from ate_history:

where id::varchar = a.code

Postgresql column reference is ambiguous

You have to use the alias name in the insert query because list has two references, one reference in EXCLUDED.list and another reference to the column for an update statement.

Please check the below query (I append the alias with name os in query):

CREATE OR REPLACE FUNCTION update_order(input_id uuid,input_sku text,input_store_id uuid,input_order_date bigint,input_asin text,input_amount int,input_list text[],input_price real,input_list2 text) RETURNS void LANGUAGE plpgsql AS
$body$
#variable_conflict use_column
BEGIN
INSERT INTO orders_summary as os (id,sku,store_id,order_date,asin,amount,list,price)
VALUES(input_id,input_sku,input_store_id,to_timestamp(input_order_date / 1000.0),input_asin,input_amount,input_list,input_price) ON CONFLICT(sku,order_date) DO UPDATE
SET list = ARRAY_APPEND(os.list, input_list2),
amount = os.amount + input_amount,
price = input_price
WHERE NOT os.list @> input_list;
END
$body$;

Or you can use table name:

CREATE OR REPLACE FUNCTION update_order(input_id uuid,input_sku text,input_store_id uuid,input_order_date bigint,input_asin text,input_amount int,input_list text[],input_price real,input_list2 text) RETURNS void LANGUAGE plpgsql AS
$body$
#variable_conflict use_column
BEGIN
INSERT INTO orders_summary (id,sku,store_id,order_date,asin,amount,list,price)
VALUES(input_id,input_sku,input_store_id,to_timestamp(input_order_date / 1000.0),input_asin,input_amount,input_list,input_price) ON CONFLICT(sku,order_date) DO UPDATE
SET list = ARRAY_APPEND(orders_summary.list, input_list2),
amount = orders_summary.amount + input_amount,
price = input_price
WHERE NOT orders_summary.list @> input_list;
END
$body$;


Related Topics



Leave a reply



Submit