Search code examples
sqlsql-servert-sqlunionidentifier

SQL The multi-part identifier "" could not be bound. UNION plus JOIN


I'm having trouble adding the field T.TEXTE_0 to the below query, I get "The multi-part identifier "T.TEXTE_0" could not be bound."

Eventually I'd like to add it to both SELECTs. Any ideas?

   SELECT B.BOMSEQ_0 BOMSEQ, 
          case when B1.ITMREF_0 IS NOT NULL then NULL else B.CPNITMREF_0 end as CPNITREF, 
          case when B1.ITMREF_0 IS NOT NULL then NULL else B.YREF_0 end as YREF, 
          B.BOMTEXNUM_0, T.TEXTE_0,
          B1.BOMSEQ_0, B1.CPNITMREF_0, B1.YREF_0, B1.BOMTEXNUM_0, NULL
    FROM [BOMD]  B
    INNER JOIN [BOMD] B1
    ON B.CPNITMREF_0=B1.ITMREF_0 
     AND B.BOMALT_0=B1.BOMALT_0
    WHERE B.ITMREF_0='50' 
      and B.BOMALT_0= 1
    UNION ALL
    SELECT B.BOMSEQ_0, B.CPNITMREF_0, B.YREF_0, B.BOMTEXNUM_0, T.TEXTE_0,
           NULL, NULL, NULL, NULL, NULL
    FROM [BOMD] B
    LEFT JOIN [TEXCLOB] AS T
    ON B.BOMTEXNUM_0=T.CODE_0
    WHERE B.ITMREF_0='50'
      and B.BOMALT_0= 1
    ORDER BY BOMSEQ, BOMSEQ_0

Solution

  • The error is in the first select in the union. You are selecting T.TEXTE_0 at the end of the first line, but you don't join that select to the TEXCLOB table. If you want that field to come out in both selects then you need to add the join to the first select clause as well.