SQL Server中

时间:2018-12-10 12:21:42

标签: select sql-server-2012 case

我想通过在where条件中使用case语句替换许多OR条件来优化以下查询。我该如何为以下where条件编写CASE语句,因为将空值传递给参数时不应在何处执行条件?

DECLARE @ALLSUBFORM VARCHAR(MAX)
DECLARE @LAN VARCHAR(10) = 'EN'
DECLARE @SUBFORMAT VARCHAR(50) = 'MTR'
DECLARE @CASNUM VARCHAR(20)

SELECT DISTINCT 
    TP.F_PRODUCT AS ID,
    TP.F_PRODUCT_NAME AS [NAME]
FROM 
    PDF TP
WHERE
    (@LAN IS NULL OR TP.F_LAN = @LAN OR @LAN = '-1')
    AND (@SUBFORMAT IS NULL OR @SUBFORMAT = '-1' OR TP.F_SUBFORMAT IN (SELECT SUBFORMAT FROM SUBFORMATTEMP))
    AND (@CASNUM IS NULL OR REPLACE(LTRIM(RTRIM(REPLACE(CASN.F_DATA,'¿',' '))),' ','; ') LIKE @CASNUM OR REPLACE(LTRIM(RTRIM(REPLACE(TP.F_CAS_NUMBERS,'¿',' '))),' ','; ') LIKE @CASNUM)
END

0 个答案:

没有答案