rules repository

G-7910

🆓
Error

Never use DML within a SQL macro.

Reason

Doing DML (except for select) within a SQL macro can lead to disastrous side effects from calling the macro in a SQL query.

Logging macro calls via a call to a procedure that does DML in an autonomous transaction can be an exception to the rule.

Example

Non-Compliant Example

create or replace package body my_package is
   function row_generator(in_num_rows in number) -- NOSONAR G-7460: non-deterministic
      return varchar2 sql_macro as
   begin
      insert into function_calls(name,called_at,parameter_value)
      values ($$plsql_unit,current_timestamp,in_num_rows);
      commit;

      return 'select level as row_sequence
             from dual 
          connect by level <= in_num_rows';
   end row_generator;
end my_package;
/
Issues
LineColumnMessage
57Insert used within a SQL macro row_generator.
77Commit used within a SQL macro row_generator.

Compliant Solution - ★★★★★

create or replace package body my_package is
   function row_generator(in_num_rows in number) -- NOSONAR G-7460: non-deterministic
      return varchar2 sql_macro as
   begin
      return 'select level as row_sequence
             from dual 
          connect by level <= in_num_rows';
   end row_generator;
end my_package;
/

References