1

When p_type is null, then I have to provide two values i.e ('value1','value2'). How can I achieve this using Case or Decode statement in where clause? Below PL/SQL block is not valid and throws an error of missing keyword: statement ignored.

Declare
l_xml CLOB;
p_type varchar2(200) := 'value1'; -- can be null
begin
SELECT XMLAGG (
            XMLELEMENT ("Address",
            (XMLFOREST (usg.code as "CODE",
            usg.date as "Date"
            )))) .getclobval()
             into l_xml
                        FROM  usg

WHERE usg.number = '12345'  
AND usg.code = p_type
OR CASE WHEN p_type IS NULL THEN usg.code in ('value1','value2') END;

dbms_output.put_line (l_xml);

end;
  • Possible duplicate of [Conditional WHERE clause in SQL Server](https://stackoverflow.com/questions/18629132/conditional-where-clause-in-sql-server) – Gauravsa Oct 02 '18 at 03:40

2 Answers2

0

You don't need a CASE in this particular instance.

WHERE usg.number = '12345'  
AND (usg.code = p_type
     OR
     (p_type IS NULL AND usg.code in ('value1','value2'))
    )
Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
-2

Case statement should be part of the select statement and not where conditions

  • 1
    (1) you can't use a case *statement* anywhere in a sql query; (2) you can use a case *expression* anywhere in a sql query where an expression is allowed, including the select and where clauses. – Jeffrey Kemp Oct 02 '18 at 03:08