I want something like this
IF EMP_ID_NULL = 'N' THEN
SELECT * FROM EMPLOYEE WHERE
EMP_ID IS NULL;
ELSE
SELECT * FROM EMPLOYEE WHERE
EMP_ID =EMP_ID_VAL;
END IF;
And i tried the above in CASE statement like below, but as expected it dint work. How do i add is null in CASE?
DECLARE EMP_ID_NULL VARCHAR2(1);
DECLARE EMP_ID_VAL NUMBER;
SELECT * FROM EMPLOYEE WHERE
AND EMP_ID ( CASE WHEN EMP_ID_NULL = 'N' THEN 'IS ' || NULL
ELSE '='|| EMP_ID_VAL END
);
One way would be:
(NOTE: did include those variables using a WITH statement, because it is unknow to me how to create those on DBFIDDLE...)
WITH tmp_value as (
SELECT
'N' as emp_id_null,
2 as emp_id_val
)
SELECT
emp_id,
emp_name,
emp_id_null
FROM
employee
CROSS APPLY tmp_value
WHERE EMP_ID_NULL ='N' and emp_id is null
UNION ALL
SELECT
emp_id,
emp_name,
emp_id_null
FROM
employee
CROSS APPLY tmp_value
WHERE EMP_ID_NULL !='N' and emp_id = emp_id_val
;
see: DBFIDDLE
EDIT: Or this (which I should have produces before doing above...)
WITH tmp_value as (
SELECT
'Y' as emp_id_null,
2 as emp_id_val
)
SELECT
emp_id,
emp_name,
emp_id_null
FROM
employee
CROSS APPLY tmp_value
WHERE (EMP_ID_NULL ='N' and emp_id is null)
or (EMP_ID_NULL !='N' and emp_id = emp_id_val);