2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
``++DECODE++`` is an old function that has been replaced by the easier to understand and more common ``++CASE++``. Unlike ``++DECODE++``, ``++CASE++`` may also be used directly within PL/SQL.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
SET SERVEROUTPUT ON
DECLARE
operand CHAR(1) := 'B';
l_result PLS_INTEGER;
BEGIN
-- Noncompliant
SELECT DECODE(operand, 'A', 1
, 'B', 2
, 'C', 3
, 'D', 4
, 'E', 5
, 'F', 6
, 7)
INTO l_result
FROM dual;
DBMS_OUTPUT.PUT_LINE('l_result = ' || l_result); -- 2
END;
/
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
SET SERVEROUTPUT ON
DECLARE
operand CHAR(1) := 'B';
l_result PLS_INTEGER;
BEGIN
l_result := CASE operand
WHEN 'A' THEN 1
WHEN 'B' THEN 2
WHEN 'C' THEN 3
WHEN 'D' THEN 4
WHEN 'E' THEN 5
WHEN 'F' THEN 6
ELSE 7
END;
DBMS_OUTPUT.PUT_LINE('l_result = ' || l_result); -- 2
END;
/
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-04-28 16:49:39 +02:00
No issue is raised when ``++DECODE++`` contains only one case (i.e. only one ``++search++`` and one ``++result++`` components) because using ``++CASE++`` would make the code less readable in this scenario.
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Replace this use of "DECODE" with "CASE".
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]