Parameter

基於 SSRS 參數的不同 where 子句

  • February 13, 2016

我有一個 SSRS 報告,其中一個參數可以取 3 個值。select 查詢在所有情況下都是相同的,但 where 子句需要根據參數值檢查不同的日期欄位。這可能在同一個查詢中嗎?

詢問:

SELECT OrdNO, OrdType, customer, SalesRep, FundDate, DisbursementDate, Branch, SalesPrice 
FROM Orders

-- Case @ReportType=1, use FundDate
  where (FundDate >= @FromDate and FundDate <= @ToDate)

-- Case @ReportType=2, use DisbursmentDate
  Where (DisbursementDate >= @FromDate and DisbursementDate <= @ToDate)

-- Case @ReportType=3, use both dates
  where (FundDate >= @FromDate and FundDate <= @ToDate) or (DisbursementDate >= @FromDate and DisbursementDate <= @ToDate)

是的,如下所示。

SELECT * 
FROM DataTable
WHERE (@SearchField = 'CreateDate' AND CreateDate BETWEEN @StartDate AND @EndDate)
OR (@SearchField = 'ModifyDate' AND ModifyDate BETWEEN @StartDate AND @EndDate)

或者,要使用您指定的範例 WHERE 子句,下面的呢?

SELECT *
FROM DataTable
WHERE (
       @ReportType = 1
       AND FundDatte >= @FromDate
       AND FunDate <= @ToDate
       )
   OR (
       @ReportType = 2
       AND DisbursementDate >= @FromDate
       AND DisbursementDate <= @ToDate
       )
   OR (
       @ReportType = 3
       AND (
           (
               FundDatte >= @FromDate
               AND FunDate <= @ToDate
               )
           AND (
               DisbursementDate >= @FromDate
               AND DisbursementDate <= @ToDate
               )
           )
       )

引用自:https://dba.stackexchange.com/questions/128825