== Why is this an issue?

Since the purpose of the ``++EXECUTE IMMEDIATE++`` statement is to execute dynamic SQL queries - which by definition can contain unexpected errors - properly handling exceptions becomes critical. Therefore, care should be taken to trap all possible exceptions.


=== Noncompliant code example

[source,sql]
----
DECLARE
  result      VARCHAR2(42);
  column      VARCHAR2(42);
BEGIN
  column := 'DUMMY_2';
  EXECUTE IMMEDIATE 'SELECT ' || column || ' FROM DUAL' INTO result; -- Non-Compliant
END;
/
----


=== Compliant solution

[source,sql]
----
SET SERVEROUTPUT ON

DECLARE
  result      VARCHAR2(42);
  column      VARCHAR2(42);
BEGIN
  column := 'DUMMY_2';
  EXECUTE IMMEDIATE 'SELECT ' || column || ' FROM DUAL' INTO result; -- Compliant
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE ('Execute immediate error: ' || DBMS_UTILITY.FORMAT_ERROR_STACK);
END;
/
----

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Add an exception trap to this execution.


endif::env-github,rspecator-view[]
