0

我有以下查询,但是当我传递此别名列名的参考名称时,它显示无效的列名我该怎么做

select OM.*,
       convert(int,Replace(Ltrim(Replace(left(OM.mstrefc,6), '0', ' ')), ' ', '0')) as partycode,
       OI.*,
       Ac.AcctName,
       Ac.acctaddr,
       UN.UnitName
from ordemst OM
  join ordeitd OI
    on OM.mstCode = OI.ItdCode
  join Account Ac 
    on OM.partycode = Ac.acctcode
  left join unitdet UN
    on OI.ItdUnit = UN.unitcode and 
       OI.CompCode = UN.CompCode 
where OM.MstCode = 47 and 
      OM.MstType =79 and 
      OM.CompCode =117 and 
      AC.compcode =117 and 
      OI.Compcode=117
4

1 回答 1

2

您不能在连接子句中使用来自 select 子句的别名。

column_alias 可用于 ORDER BY 子句,但不能用于 WHERE、GROUP BY 或 HAVING 子句

显然不在连接子句中。

您必须用真实的东西替换别名:

join Account Ac 
on convert(int,Replace(Ltrim(Replace(left(OM.mstrefc,6), '0', ' ')), ' ', '0')) = Ac.acctcode

取自 MSSQL 文档

于 2012-10-31T10:54:55.753 回答