1
我正在創建一個報告,用戶可以在兩個狀態字段之間進行選擇。默認Completed
狀態和Any
。如果選擇的狀態不是Completed
,那麼它需要忽略或選擇所有值。我創建了一個包含所有不同值的臨時表,並將其設置爲局部變量。我試圖在我的where
子句中使用case statement
和這個臨時表來處理這個問題,但它不會讓我。除了將sql
設置爲字符串和exec'ing
之外,還有更好的方法來處理嗎?在where子句中使用臨時表時遇到問題
Declare @paramCompleted varchar(20) = 'Completed'
Declare @paramCommitmentExamined varchar(20) = 'Requested'
Declare @paramDateFrom Date
Declare @paramDateTo Date
Declare @PurchStatus as table([status] varchar(15))
insert into @PurchStatus([status]) select DISTINCT TX01STAT from dimFile_T order by TX01STAT
--select * from @PurchStatus
SELECT
dbo.ParseDate(O.ORDDATE) AS ORDDATE,
T.TX04STAT,--Commitment Examined & type
E.EXAMINER,
T.TX02STAT,--Outside search
C.COUNTY,
S.SETTSTAT,
CASE
WHEN L.LOANAMT = 0 THEN 'Cash'
ELSE CAST(L.LOANAMT AS VARCHAR(10))
END AS LOANAMT,
T.TX01STAT,--Purchase agreement received
M.MKTSOURC,
CASE
WHEN C.FIRMFILE LIKE '%PC%' THEN 'Commercial'
WHEN C.FIRMFILE LIKE '%RC%' THEN 'Commercial'
ELSE 'Residential'
END AS FIRMFILE,
C.FIRMFILE as FILENUM,
'Metes & Bounds' AS LOTUNIT,
S.STATCMT
FROM
dimFile_O O
LEFT JOIN dimfile_T T ON T.FIRMFILE = O.FIRMFILE
LEFT JOIN dimfile_E E ON E.FIRMFILE = O.FIRMFILE
LEFT JOIN dimfile_C C ON C.FIRMFILE = O.FIRMFILE
LEFT JOIN dimfile_S S ON S.FIRMFILE = O.FIRMFILE
LEFT JOIN dimfile_L L ON L.FIRMFILE = O.FIRMFILE
LEFT JOIN dimfile_M M ON M.FIRMFILE = O.FIRMFILE
LEFT JOIN dimfile_P P ON P.FIRMFILE = O.FIRMFILE
WHERE
T.TX04STAT IN (@paramCommitmentExamined)
AND T.TX01STAT IN (
CASE
WHEN @paramCompleted = 'Completed' THEN @paramCompleted
ELSE @PurchStatus
END)
我沒想到像那樣分組他們。完美的作品。謝謝! – NikZ