Let say I have this table,
A B
-------
1 A
2 C
3 A
4 NULL
5 B
6 A
I have only a single parameter in stored procedure. I need to pass a single parameter @param. I need to select all rows equal to A or select all rows not equal to A. I have only a single parameter @param. If @param = ‘A’ select all rows with A and If @param <> ‘A’ select all rows with not equal to A.
Edit:
Here is 1 approach I have found. Not sure which approach is best one.
DECLARE @Param VARCHAR(5)='NOt A'
DECLARE @tbl TABLE(A INT,B VARCHAR(5))
INSERT INTO @tbl(A,B)
VALUES
(1,'A'),
(2,'C'),
(3,'A'),
(4,NULL),
(5,'B'),
(6,'A')
SELECT *
FROM @tbl
WHERE 1 =
(
CASE WHEN @param = 'A'
THEN
(
CASE WHEN B = 'A' THEN 1 ELSE 2 END
)
ELSE
(
CASE WHEN (B IS NULL OR B <> 'A') THEN 1 ELSE 2 END
)
END
)
So if I understand you correctly, you basically want to select all rows equal to
Aif the value of@paramis indeedA– and all other rows if it’s notA??Something like: