解决SQL Server中的“多部分标识符无法绑定”错误

时间:2011-07-25 12:52:07

标签: sql-server-2005

select distinct 
  l.username,
  p.payid,
  p.paymentdate,
  sum(p.paymentamount) as payment,
  b.balance as balance 
from 
  tblUserLoginDetail l,
  tblInvoicePaymentDetails p 
  left outer join tblPaymentCustomerBalance b 
    on p.accountnumber=10009 
    and p.payid=b.payid 
    and p.customerid=l.loginid
  group by  p.payid,p.paymentdate,b.balance,l.username

错误是:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "l.loginid" could not be bound.

解决方案是什么?

1 个答案:

答案 0 :(得分:2)

您在FROM子句中的tblUserLoginDetail和tblInvoicePaymentDetails之间有交叉连接,因此您不能在FROM子句中使用l.loginid

我认为你想要的是一个明确的INNER JOIN。我也将过滤和连接条件分开:

select
    l.username,
    p.payid,
    p.paymentdate,
    sum(p.paymentamount) as payment,
    b.balance as balance
from
    tblUserLoginDetail l
    inner join
    tblInvoicePaymentDetails p On p.customerid=l.loginid 
    left outer join
    tblPaymentCustomerBalance b ON p.payid=b.payid
where
    p.accountnumber=10009
group by
   p.payid,p.paymentdate,b.balance,l.username