如何修复SQL查询错误“无效列名”?

时间:2014-10-15 07:15:47

标签: sql-server vba

我在SQL服务器上的VBA中运行以下查询。为了简化,我在select子句中为不同的列使用别名。 VBA返回以下错误:

  

无效的列名称' QtyDCo'。

我尝试添加括号和引号,但它没有帮助。有没有人知道如何修复错误?

"SELECT Qry_ETD_Pos_Combined.colDate AS [Date], " & _
    " Qry_ETD_Pos_Combined.colClrBroker AS [Clr Broker], " & _
    " Qry_ETD_Pos_Combined.colAccount AS Account, " & _
    " tblDefProd.colInstr AS [FT/OP], tblDefProd.colCcy, " & _
    " Qry_ETD_Pos_Combined.colId AS Instrument,  tblDefDeriv.colExpiry, " & _
    " tblDefProd.colMultiplier AS Multiplier, " & _
    " IsNull(tblDatPositionsCalc.colLots,0) AS QtyDCo, " & _
    " IsNull(tblDatPositions.colLots,0) AS QtyBrk, " & _
    " QtyDCo - QtyBrk AS [Qty Diff], " & _
    " IsNull(tblDatPositions.colMktPrice,0) AS [Price(ETD)], " & _
    " IsNull(tblDatDeriv.colPrevClose,0) AS [Price(BBG)], " & _
    " [Price(ETD)]-[Price(BBG)] AS [Mkt Price Diff], " & _
    " Round(IsNull(tblDatPositionsCalc.colPrice,0),8) AS [Avg Price D&Co], " & _
    " IsNull(tblDatPositions.colPrice,0) AS [Avg Price Brk], " & _
    " Round([Avg Price D&Co]-[Avg Price Brk],5) AS [Avg Price Diff], " & _
    " IIf([Qty D&Co]=0,0,Round(IIf([FT/OP]='FT',tblDatPositionsCalc.colLots*tblDefProd.colMultiplier*([Price(ETD)]-[Avg Price D&Co]),IIf([FT/OP]='OP',tblDatPositionsCalc.colLots*tblDefProd.colMultiplier*IsNull([Price(ETD)],[Price(BBG)]),0)),2)) AS [Mkt Val D&Co], IsNull(tblDatPositions.colVarMar,0) AS [Mkt Val Brk], Round([Mkt Val D&Co]-[Mkt Val Brk],2) AS [Mkt Val Diff], " & _
    "Round(IIf([FT/OP]='OP',-[Qty D&Co]*[Multiplier]*[Avg Price D&Co],0),2) AS [Trade Val D&Co], Round(IIf([FT/OP]='OP',-[Qty Brk]*[Multiplier]*[Avg Price Brk],0),2) AS [Trade Val Brk], [Trade Val D&Co]-[Trade Val Brk] AS [Trade Val Diff], tblDatAdmFX.colSpot AS FX, " & _
    "Round([Mkt Val D&Co]*[FX],2) AS [Mkt Val CHF], Round([Trade Val D&Co]*[FX],2) AS [Trade Val CHF] " & _
    "FROM (((((" & MyQryPosComb & " AS Qry_ETD_Pos_Combined LEFT JOIN dbOps.dbo.tblDatPositions ON (Qry_ETD_Pos_Combined.colId = tblDatPositions.colId) " & _
    "AND (Qry_ETD_Pos_Combined.colAccount = tblDatPositions.colAccount) " & _
    "AND (Qry_ETD_Pos_Combined.colDate = tblDatPositions.colDate)) LEFT JOIN dbOps.dbo.tblDatPositionsCalc ON (Qry_ETD_Pos_Combined.colId = tblDatPositionsCalc.colId) " & _
    "AND (Qry_ETD_Pos_Combined.colAccount = tblDatPositionsCalc.colAccount) " & _
    "AND (Qry_ETD_Pos_Combined.colDate = tblDatPositionsCalc.colDate)) LEFT JOIN dbProd.dbo.tblDefDeriv ON Qry_ETD_Pos_Combined.colId = tblDefDeriv.colId) LEFT JOIN dbProd.dbo.tblDefProd ON tblDefDeriv.colProduct = tblDefProd.colProduct) LEFT JOIN dbMktData.dbo.tblDatDeriv ON (Qry_ETD_Pos_Combined.colDate = tblDatDeriv.colDate) " & _
    "AND (Qry_ETD_Pos_Combined.colId = tblDatDeriv.colId)) LEFT JOIN dbMktData.dbo.tblDatFX ON Qry_ETD_Pos_Combined.colDate = tblDatFX.colDate " & _
    "WHERE (tblDatFX.colCcy1=[tblDefProd].[colCcy] AND tblDatFX.colCcy2='CHF');"

1 个答案:

答案 0 :(得分:0)

您需要使用子查询或CTE首先定义 QtyDCo列,然后在外部/最终查询中引用它。

SQL被定义为SELECT子句中的所有表达式都被计算出来#34;就像"它们全部并行计算 1 。因此,您无法在同一SELECT子句中引用您也定义的列。

1 与...相反被定义为从左到右计算。