File tree Expand file tree Collapse file tree 5 files changed +49
-0
lines changed
4-language-usage/7-stored-objects/9-sql-macros Expand file tree Collapse file tree 5 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ title: SQL Macros
Original file line number Diff line number Diff line change 1+ # G-7910: Never use DML within a SQL macro.
2+
3+ !!! danger "Critical"
4+ Reliability, Testability
5+
6+ ## Restriction
7+
8+ Oracle Database 21c (19c from version 19.7 for table macros alone)
9+
10+ ## Reason
11+
12+ Doing DML (except for ` select ` ) within a SQL macro can lead to disastrous side effects from calling the macro in a SQL query.
13+
14+ Logging macro calls via a call to a procedure that does DML in an autonomous transaction can be an exception to the rule.
15+
16+ ## Example (bad)
17+
18+ ``` sql
19+ create or replace function row_generator (
20+ num_rows_in in number (32 ,0 )
21+ )
22+ return varchar2 sql_macro as
23+ begin
24+ insert into function_calls(name, called_at, parameter_value)
25+ values ($$PLSQL_UNIT, current_timestamp , num_rows_in);
26+ commit ;
27+
28+ return ' select level as row_sequence from dual connect by level <= num_rows_in' ;
29+ end row_generator;
30+ /
31+ ```
32+
33+ ## Example (good)
34+
35+ ``` sql
36+ create or replace function row_generator (
37+ num_rows_in in number (32 ,0 )
38+ )
39+ return varchar2 sql_macro as
40+ begin
41+ return ' select level as row_sequence from dual connect by level <= num_rows_in' ;
42+ end row_generator;
43+ /
44+ ```
Original file line number Diff line number Diff line change @@ -124,6 +124,7 @@ n/a | 7720 | Never use multiple UPDATE OF in trigger event clause. | Blocker |
124124n/a | 7730 | Avoid multiple DML events per trigger. | Minor | | | ✘ ; | | | | | ✘ ;
125125n/a | 7740 | Never handle multiple DML events per trigger if primary key is assigned in trigger. | Major | | ✘ ; | | | ✘ ; | | |
126126n/a | 7810 | Never use SQL inside PL/SQL to read sequence numbers (or SYSDATE). | Major | | ✘ ; | ✘ ; | | | | |
127+ n/a | 7910 | Never use DML within a SQL macro. | Critical | | | | | ✘ ; | | | ✘ ;
12712878 | 8110 | Never use SELECT COUNT(* ) if you are only interested in the existence of a row. | Major | | ✘ ; | | | | | |
128129n/a | 8120 | Never check existence of a row to decide whether to create it or not. | Major | | ✘ ; | | | ✘ ; | | |
12913079 | 8210 | Always use synonyms when accessing objects of another application schema. | Major | ✘ ; | | ✘ ; | | | | |
Original file line number Diff line number Diff line change 222222 [id = "object-types" ],
223223 [id = "triggers" ],
224224 [id = "sequences" ],
225+ [id = "sql-macros" ],
225226 [id = "patterns" ],
226227 [id = "access-objects-of-foreign-application-schemas" ],
227228 [id = "validating-input-parameter-size" ],
Original file line number Diff line number Diff line change @@ -156,6 +156,8 @@ write_text "### Triggers"
156156write_guidelines " 4-language-usage/7-stored-objects/7-triggers" " ####"
157157write_text " ### Sequences"
158158write_guidelines " 4-language-usage/7-stored-objects/8-sequences" " ####"
159+ write_text " ### SQL Macros"
160+ write_guidelines " 4-language-usage/7-stored-objects/9-sql-macros" " ####"
159161write_text " ## Patterns"
160162write_text " ### Checking the Number of Rows"
161163write_guidelines " 4-language-usage/8-patterns/1-checking-the-number-of-rows" " ####"
You can’t perform that action at this time.
0 commit comments