2018年1月5日 星期五

SQL Case When In Where Condition

SELECT
   column1, 
   column2
FROM
   viewWhatever
WHERE
CASE 
    WHEN @locationType = 'location' AND account_location = @locationID THEN 1
    WHEN @locationType = 'area' AND xxx_location_area = @locationID THEN 1
    WHEN @locationType = 'division' AND xxx_location_division = @locationID THEN 1
    ELSE 0
END = 1
SELECT column1, column2
FROM viewWhatever
WHERE
    (@locationType = 'location' AND account_location = @locationID)
    OR
    (@locationType = 'area' AND xxx_location_area = @locationID)
    OR
    (@locationType = 'division' AND xxx_location_division = @locationID)
By: https://stackoverflow.com/questions/206484/sql-switch-case-in-where-clause

沒有留言:

張貼留言

remove last word from SQL query

SET @columnSql = SUBSTRING ( @columnSql , 1 , LEN ( @columnSql ) - 1 )