2017-08-08 103 views
0

我使用SQL Server Management Studio版本11.0.3128.0與SQL Server 2012的左連接上的SQL Server Management Studio語法錯誤?

我有這個疑問,當我運行它,但SSMS口口聲聲說是我的SQL語法錯誤的作品,雖然它運行良好。該錯誤是

多部分標識符bsr.budgetId無法綁定

和第2行上

SELECT 
    bh.cpNo as CodigoDoPedido, 
    df.registerDate as DataFechamento, 
    meh.pedidoCliente as NumeroDePedidoDoCliente, 
    bh.deliveryDate as DataEntrega, 
    ch.clientText as NomeCliente, 
    ah.addressCep as CEPEnderecoCliente, 
    ah.addressDistrict as DistritoEnderecoCliente, 
    ah.addressInfo as InfoEnderecoCliente, 
    ah.addressInfo2 as Info2EnderecoCliente, 
    ah.addressInfo3 as Info3EnderecoCliente, 
    ah.addressText as TextEnderecoCliente, 
    cih.cityText as CidadeEnderecoCliente, 
    sh.stateText as EstadoEnderecoCliente, 
    coh.countryText as PaisEnderecoCliente, 
    comh.commInfo as TelefoneCliente, 
    buh.buildText as ProjetoOrcamento, 
    bh.valorCustoDocumentacao as CustoDocumentacaoOrcamento, 
    bh.valorTotalEstrangeiro as ValorTotalEstrangeiroOrcamento 
from 
    _budget_hdr as bh 
    left join (
        select 
      bsr2.budgetId, 
      min(bsr2.registerId) as registerId  
        from _budgetStatus_reg as bsr2 
        where bsr2.budgetStatusId = 4  
        group by bsr2.budgetId 
    ) AS bsr on 
     bsr.budgetId = bh.budgetId 
    left join _budgetStatus_reg as df on 
     df.budgetId = bsr.budgetId AND 
     df.registerId = bsr.registerId  
    left join _minutaEletronica_hdr as meh on 
     meh.minutaId = bh.minutaId 
    left join _client_hdr as ch on 
     ch.clientId = bh.clientId 
    left join _clientAddress_rel as car on 
     car.clientId = ch.clientId and 
     car.addressTypeId = 0 
    left join _address_hdr as ah on 
     ah.addressId = car.addressId 
    left join city_hdr as cih on 
     cih.cityId = ah.cityId 
    left join state_hdr as sh on 
     sh.stateId = ah.stateId 
    left join country_hdr as coh on 
     coh.countryId = ah.countryId 
    left join _clientComm_rel as ccr on 
     ccr.clientId = ch.clientId 
    left join _comm_hdr as comh on 
     comh.commId = ccr.commId 
    left join build_hdr as buh on 
     buh.buildId = bh.buildId 
where 
    bh.budgetId = 76089; 

我一個

語法錯誤放棄,我真的不知道我的SQL有什麼問題。

編輯

我改變了重複別名BSR2,它並沒有解決問題

+0

左連接上的子查詢具有相同的別名'bsr'。刪除裏面的子查詢中的別名,我認爲這將解決它。 –

+0

您正在使用bsr兩次,也許使用其他名稱 – Alam

+0

更改了別名,相同問題 –

回答

0

的問題是,您使用的是相同的別名,BSR,兩次。一旦之內第一個左側加入您的查詢,並且第二次您在該連接中別名派生表。改變其中一個,你應該沒問題。

+0

更改別名,相同問題 –

+0

@AndréMarcondesTeixeira您可以將5行虛擬數據放在一起並將其粘貼到SQLFiddle或rextester中嗎? – Eli

0

對兩個完全不同的表達式使用'bsr'別名。我敢打賭,這是導致錯誤的原因。

+0

更改了別名,相同的問題 –

相關問題