'join'附近的语法不正确

时间:2013-08-22 14:38:59

标签: tsql

我有这样的代码:

  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

在尝试运行它时,它给出了关于'join'附近的错误语法的错误。不幸的是,我不知道为什么会出现这样的错误..

3 个答案:

答案 0 :(得分:7)

JOINFROM子句的一部分。 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 :(得分:1)

join出现在where之前。

  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)

答案 2 :(得分: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)