/ Published in: SQL
How to get a query based on case/when sintax, on WHERE statement (Treated as boolean expresion)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
SET NOCOUNT ON DECLARE @Sede VARCHAR(10) SET @Sede = 'TD' DECLARE @listas TABLE(Origen VARCHAR(2)) INSERT @listas VALUES('AF') INSERT @listas VALUES('CP') INSERT @listas VALUES('LL') INSERT @listas VALUES('EC') INSERT @listas VALUES('PE') INSERT @listas VALUES('ZZ') SELECT * FROM @listas WHERE 1 = CASE WHEN @Sede = 'TD' THEN CASE WHEN Origen IN ('AF','CP','LL','EC','PE') THEN 1 ELSE 0 END WHEN @Sede = 'SS' THEN CASE WHEN Origen IN ('LL','EC','PE') THEN 1 ELSE 0 END ELSE CASE WHEN Origen = @Sede THEN 1 ELSE 0 END END SET NOCOUNT OFF