Domanda

I have a more complex query with inner join that throws the error:

[Error Code: -206, SQL State: 42703] DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=TT.ALIAS, DRIVER=4.13.127. 2) [Error Code: -727, SQL State: 56098] DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-206;42703;TT.ALIAS, DRIVER=4.13.127

the simple version of the query that doesn't work is:

select column1 as alias from table1 tt
inner join table1 ts
on tt.alias = ts.column1

this simple query doesn't make sense but is a simple version of the query I'm trying to run. This was supposed to run with no errors right? If I remove the alias it runs with no problem..

Thanks in advance

È stato utile?

Soluzione

You cannot use a column alias in an on or where clause. You can do:

select column1 as alias
from table1 tt inner join
     table1 ts
     on tt.column1 = ts.column1;

You could also do:

select alias
from (select column1 as alias
      from table1
     ) tt inner join
     table1 ts
     on tt.alias = ts.column1;

During the compile phase, the queries are evaluated by clause, with the from clause first, then then where and select. In other words, the compiler doesn't "know" what is in the select when it evaluates the from.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top