2013-08-22 112 views
-2

我有這樣的代碼:附近有語法錯誤「加入」

Select 
    C.ClientId 
    ,K.NameShort 
    ,C.SetId 
    ,C.CaseId 
    ,C.ClientCaseNumber 
    ,D.Name 
    ,Pay.Overpayment 
    from CaseDetails as C 
    where (C.ClientId = @ClientId or @ClientId IS NULL) and (C.SetId = @SetId or @SetId IS NULL) 

    join Client as K on C.ClientId = K.ClientId 
    join DocumentDetails as DD on C.CaseDetailsId = DD.CaseDetailsId 
    join PaymentDocumentHistory as Pay on DD.DocumentId = Pay.DocumentId 
    join DebtorDetails as D on C.CaseDetailsId = D.CaseDetailsId 

試圖運行它,它給了我錯誤關於不正確的語法附近「加入」。不幸的是,我不知道爲什麼會出現這樣的錯誤。

+1

看看t-sql'SELECT'語法[這裏](http://technet.microsoft.com/en-us/library/ms189499.aspx) – AgentSQL

回答

7

JOIN s爲的FROM子句的一部分。 WHERE子句位於(整個)FROM子句之後。

Select 
    C.ClientId 
    ,K.NameShort 
    ,C.SetId 
    ,C.CaseId 
    ,C.ClientCaseNumber 
    ,D.Name 
    ,Pay.Overpayment 
    from CaseDetails as C 
    join Client as K on C.ClientId = K.ClientId 
    join DocumentDetails as DD on C.CaseDetailsId = DD.CaseDetailsId 
    join PaymentDocumentHistory as Pay on DD.DocumentId = Pay.DocumentId 
    join DebtorDetails as D on C.CaseDetailsId = D.CaseDetailsId 
    where (C.ClientId = @ClientId or @ClientId IS NULL) and (C.SetId = @SetId or @SetId IS NULL) 
1

joinwhere之前。

from CaseDetails as C  
    join Client as K on C.ClientId = K.ClientId 
    join DocumentDetails as DD on C.CaseDetailsId = DD.CaseDetailsId 
    join PaymentDocumentHistory as Pay on DD.DocumentId = Pay.DocumentId 
    join DebtorDetails as D on C.CaseDetailsId = D.CaseDetailsId 
    where (C.ClientId = @ClientId or @ClientId IS NULL) and (C.SetId = @SetId or @SetId IS NULL) 
0

您的JOINS不合適。您已將JOIN放在WHERE子句之後。

Select C.ClientId 
    ,K.NameShort 
    ,C.SetId 
    ,C.CaseId 
    ,C.ClientCaseNumber 
    ,D.Name 
    ,Pay.Overpayment 
    from CaseDetails as C 
    join Client as K on C.ClientId = K.ClientId 
    join DocumentDetails as DD on C.CaseDetailsId = DD.CaseDetailsId 
    join PaymentDocumentHistory as Pay on DD.DocumentId = Pay.DocumentId 
    join DebtorDetails as D on C.CaseDetailsId = D.CaseDetailsId 
    where (C.ClientId = @ClientId or @ClientId IS NULL) and (C.SetId = @SetId or @SetId IS NULL)