Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This query runs perfectly well on postgress and returns 2 columns that I am looking for:

SELECT  w.jobnr, w.ordernr
FROM
    (SELECT jobnr, ordernr FROM userdata.WIP_Data WHERE Year=2015 AND period=1) AS W
    LEFT OUTER JOIN
    (SELECT jobnr, ordernr FROM userdata.WIP_Data WHERE Year=2015 AND period=2) AS P 
    ON W.Jobnr=P.Jobnr;

It returns:
enter image description here

But when I enclose this query in a function as under:

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 jobnr, ordernr FROM userdata.WIP_Data WHERE Year=2015 AND period=1) AS W

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

and execute it by SELECT * from userdata.test3() I am getting following error:

ErrorCode: -2147467259
Severity: ERROR, Code: 42702, Line: 1076, Position:
ErrorMessage: column reference "jobnr" is ambiguous
Detail: It could refer to either a PL/pgSQL variable or a table column.

Any idea what is wrong here and how can I resolve it? Thanks

like image 898
WSK Avatar asked May 19 '17 16:05

WSK


People also ask

How can you fix an ambiguous column reference error?

You may see an error that says something like Column 'id' in field list is ambiguous . This error means that there is a field name that is present in more than one table, so it needs to be scoped with the table name to avoid ambiguity: using orders.id instead of just id will resolve the issue.

How do I solve the ambiguous name column error in SQL?

One of the simplest ways to solve an “ambiguous name column” error — without changing column name — is to give the tables you want to join an alias. This sends a clear information to the SQL Machine the columns are different. Happy querying.

What is excluded in PostgreSQL?

PostgreSQL excludes statements in PostgreSQL is used to compare any two rows from the specified column or expression by using the operator specified in PostgreSQL. At the time of excluding the column, the comparison operator will return the null or false value as output.


1 Answers

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$
like image 76
Andreas Avatar answered Sep 20 '22 17:09

Andreas