-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

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

4

3 回答 3

7

JOINs 是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)
于 2013-08-22T14:41:17.740 回答
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)
于 2013-08-22T14:41:01.753 回答
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)
于 2013-08-22T14:42:27.023 回答