Get Started
- CodeAnt AI
- Control Center
- Pull Request Review
- IDE
- Compliance
- Anti-Patterns
- Code Governance
- Infrastructure Security Database
- Application Security Database
Plsql
Jump statements, such as RETURN and CONTINUE
let you change the default flow of program execution, but jump statements that direct the control flow to the original direction are just a waste of keystrokes.
Naming custom exceptions the same as predefined ones, while technically acceptable, is not a good practice.
Multi-line comments are more readable when each line is aligned using the ”*” character. At most one violation is created for each comment
The FETCH … INTO statement is inefficient when used in a loop (where many records are expected). It leads to many context-switches between the SQL and PL/SQL engines. Instead, the FETCH … BULK COLLECT INTO
statement will issue the SQL requests in bulk, minimizing context switches.
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.
For better readability, column aliases should be used with the AS
keyword. If it is missing, it could be misread as another column being selected.
Having a bunch of standalone functions or procedures reduces maintainability because it becomes harder to find them and to see how they are related. Instead, they should be logically grouped into meaningful packages.
Functions with OUT parameters are complex to understand. Indeed, it is impossible to tell, just by looking at the function call, whether an argument is a input or output. Moreover, functions with OUT
parameters cannot be called from SQL. It is better to either break such functions up into smaller ones, which each return a single value, or to return several values at once, by combining them in a collection, record, type, or table row.
Labeled loops are useful, especially when the code is badly indented, to match the begin and end of each loop. When loops are nested, labeling them can improve the code’s readability. This rule detects nested loops which do not have a start label.
FORMS_DDL
command, like every DDL statements, is performing an implicit COMMIT. It should be used only if there is no pending transaction otherwise this transaction is automatically committed without updating the Form statuses. Also, the potentially acquired locks are lost in case of this implicit COMMIT.
”FORMS_DDL(‘COMMIT’)” and “FORMS_DDL(‘ROLLBACK’)” should be used with care and most of the time, “COMMIT_FORM” or “ROLLBACK_FORM” should be preferred.
Check the Oracle Forms documentation for more details.
Labeled blocks are useful, especially when the code is badly indented, to match the begin and end of each block. This rule verifies that block start and end labels match, when both are specified.
WHERE clause conditions that reinforce or contradict the definitions of their columns are useless; they are always either unconditionally true or unconditionally false. For instance, there’s no point in including AND column IS NOT NULL
if the column is defined as non-null.
Noteworthy
This rule raises issues only when a Data Dictionary is provided during the analysis. See https://docs.sonarqube.org/latest/analysis/languages/plsql/
NATURAL JOIN
is a type of equi-join which implicitly compares all identically-named columns of the two tables. While this a feature which may seem convenient at first, it becomes hard to maintain over time.
Consider an EMPLOYEE table with the columns FULL_NAME, and DEPT_ID, and a DEPARTMENT table with the columns DEPT_ID, and NAME. A natural join between those tables will join on the DEPT_ID column, which is the only identically-named column.
But, if a new NAME column is later added to the EMPLOYEE table, then the join will be done on both DEPT_ID and NAME. Natural joins make simple changes such as adding a column complicated and are therefore better avoided.
Shared coding conventions allow teams to collaborate efficiently. This rule checks that object attribute names match the provided regular expression.
Procedures, unlike functions, do not return values. The RETURN statement therefore, when used within a procedure, is used to prematurely end the procedure. However, having multiple exit points (i.e. more than the END
of the procedure itself), increases the complexity of the procedure and makes it harder to understand and debug.
When a single primitive is tested against three or more values in an IF, ELSIF chain, it should be converted to a CASE
instead for greater readability.
The `TO_NUMBER function is converting the value of BINARY_FLOAT, BINARY_DOUBLE, CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of NUMBER datatype.
Without providing the format of the input, TO_NUMBER will consider the provided value is compliant with the default format. Relying on a default configuration is a source of error because it creates a dependency between the code and the configuration of the ORACLE server where this code is deployed.
The behaviour of the TO_NUMBER` function will certainly not be the expected one if the configuration of the ORACLE server is changing.
Shared coding conventions allow teams to collaborate efficiently. This rule checks that type names match the provided regular expression.
Since Oracle 11.2, RELIES_ON
has been deprecated because the dependencies of result cache-enabled functions are automatically computed.
NVL was introduced by Oracle Database during the 80’s. COALESCE is a more modern function part of ANSI-92 standard than can replace NVL. Use of COALESCE should be preferred to NVL
for performance reason if the two parameters provided have the same type. COALESCE is running faster than NVL thanks to its short-circuit evaluation of the parameters.
In order to avoid “ORA-00932: inconsistent datatypes” error, double-check the two arguments of the NVL function have the same type before switching to COALESCE.
EXECUTE IMMEDIATE
is easier to use and understand than the DBMS_SQL package’s procedures. It should therefore be preferred, when possible.
The DML events clause of a trigger is not meant to be used with multiple `OF conditions. When it is, only the last one will actually be taken into account, without any error message being produced. This can lead to counter-intuitive code.
Only the UPDATE event should have an OF` condition, and there should be at most one occurence of it.
FOR and WHILE loops are structured control flow statements.
A FOR loop will iterate once for each element in the range, and the WHILE iterates for as long as a condition holds.
However, inserting an EXIT
statement within the loop breaks this structure, reducing the code’s readability and making it harder to debug.
The use of %TYPE and %ROWTYPE
is an easy way to make sure that a variable’s type always matches the type of the relevant column in an existing table. If the column type changes, then the variable changes with it.
A loop with at most one iteration is equivalent to the use of an `IF statement to conditionally execute one piece of code. No developer expects to find such usage of a loop statement. If the initial intention of the author was really to conditionally execute one piece of code, an IF statement should be used in place.
At worst that was not the initial intention of the author and so the body of the loop should be fixed to use the nested RETURN, EXIT, RAISE or GOTO` statements in a more appropriate way.
The `TO_DATE and TO_TIMESTAMP functions are converting char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to respectively a value of DATE or TIMESTAMP datatype.
Without providing the format of the input char, TO_DATE will consider the provided char is compliant with the default date format. Relying on a default configuration is a source of error because it creates a dependency between the code and the configuration of the ORACLE server where this code is deployed.
According to the Oracle’s documentation “the default date format is determined implicitly by the NLS_TERRITORY initialization parameter or can be set explicitly by the NLS_DATE_FORMAT parameter.”. It means the behaviour of the TO_DATE` function will certainly not be the expected one if the configuration of the ORACLE server is changing.
`SELECT queries that return too many columns may be complex or difficult to maintain.
This rule identifies queries that SELECT` more than the specified number of columns.
The output of DBMS_OUTPUT.PUT_LINE is not always visible, for example when SERVEROUTPUT is set to OFF
. Moreover, there is no standardized way to specify the importance of the message. It is better to use a logging mechanism instead.
Constants must be immediately initialized at declaration. They cannot be reassigned any value after the declaration, as they are constant. This rule prevents PLS-00322 exceptions from being raised at runtime.
The following code snippet illustrates this rule:
cursor%NOTFOUND is clearer and more readable than NOT cursor%FOUND
, and is preferred.
Some types cannot be constrained, and attempting to do so results in the exception PLS-00566: type name ”…” cannot be constrained
being raised.
Each function and procedure should be documented with a comment either just before or right after the IS or AS
keyword it to explain its goal and how it works.
String data types, such as VARCHAR2 or NVARCHAR2 require a size constraint. Omitting the size results in the exception PLS-00215: String length constraints must be in range (1 .. 32767)
being raised.
Complex chains of IF, ELSIF and ELSE statements should be replaced by the more readable CASE one. A complex IF statement has either several ELSIF clauses, or both an ELSIF and an ELSE clause.
Shared coding conventions allow teams to collaborate efficiently. This rule checks that all record field names match the provided regular rexpression.
Developers should use the FROM … OUTER JOIN syntax rather than the Oracle join operator (). The reason is that outer join queries that use are subject to several restrictions which do not apply to the FROM … OUTER JOIN syntax. For instance, a WHERE condition containing the + operator cannot be combined with another condition using the OR
logical operator.
Before trapping all possible exceptions, it is best to try to trap the specific ones and try to recover from those.
Full outer joins aren’t in common use, and as a result many developers don’t really understand them. Therefore, each use of this language feature should be reviewed.
SYNCHRONIZE was introduced by Oracle as a recovery mechanism in case there is a loss of synchronization between what is in memory and what is displayed. It works but it comes with a price; each call to SYNCHRONIZE
generates a network round-trip between the server and the Forms client. Most of the time it should be avoided or used with caution as explicitly stated in the Oracle Forms documentation.
Procedures which don’t accept parameters are likely to either not be reused that often or to depend on global variables instead. Refactoring those procedures to take parameters will make them both more flexible and reusable.
Labeled loops are useful, especially when the code is badly indented, to match the begin and end of each loop. However, those labels, if used, must appear on the same line as the “END” keyword in order to avoid any confusion. Indeed, the label might otherwise be seen as a procedure call.
Any insert which omits a value for a NOT NULL
column in a database table will be automatically rejected by the database unless a default value has been specified for the column.
Noteworthy
This rule raises issues only when a Data Dictionary is provided during the analysis. See https://docs.sonarqube.org/latest/analysis/languages/plsql/
Ensure that every possible exception is caught by using a WHEN OTHERS
clause.
`UNION is significantly less performant compared to UNION ALL because it removes duplicated entries and run an internal DISTINCT to achieve this.
UNION ALL is not removing duplicates and returns all the results from the queries. It performs faster in most of the cases compared to UNION. Nevertheless, the quantity of data returned by UNION ALL can be significantly more important than with UNION. On slow network, the performance gain to use UNION ALL instead of UNION can be challenged by the time lost to transfer more data than with UNION`.
Labeled blocks are useful, especially when the code is badly indented, to match the begin and end of each block. This check detects labeled blocks which are missing an ending label.
By default, the parameter mode is IN
. However, specifying it explicitly makes the code easier to read.
The use of GOTO in general is arguable. However, when used within loops, GOTO
statements are even more evil, and they can often be replaced by other constructs.
Using the same value on either side of a binary operator is almost always a mistake. In the case of logical operators, it is either a copy/paste error and therefore a bug, or it is simply wasted code, and should be simplified.
This rule ignores operators +, * and ||, and expressions: 1=1, 1<>1, 1!=1, 1~=1 and 1^=1
.
The LONG and LONG RAW datatypes are deprecated and Oracle recommends to migrate them to the LOB datatypes CLOB, NCLOB or BLOB
.
A CASE and a chain of IF/ELSIF statements is evaluated from top to bottom. At most, only one branch will be executed: the first one with a condition that evaluates to true
.
Therefore, duplicating a condition automatically leads to dead code. Usually, this is due to a copy/paste error. At best, it’s simply dead code and at worst, it’s a bug that is likely to induce further bugs as the code is maintained, and obviously it could lead to unexpected behavior.
When creating a function, procedure, package, package body, type, type body, trigger or library, it is a good practice replace the existing one to avoid errors.
Some Oracle packages contain powerful SYS-owned functions that can be used to perform malicious operations. For instance, DBMS_SYS_SQL.PARSE_AS_USER
can be used to execute a statement as another user.
Most programs do not need those functions and this rule helps identify them in order to prevent security risks.
Anchored types, i.e. those specified using either %TYPE or %ROWTYPE, cannot be constrained. Trying to do so results in the exception PLS-00573: cannot constrain scale, precision, or range of an anchored type being raised.
Each function and procedure parameter name must match a given regular expression.
Shared coding conventions allow teams to collaborate efficiently. This rule checks that cursor parameters match the provided regular expression.
Pipelined functions offers the ability to create programmatically generated tables.
One of the benefits of such functions is that they reduce memory consumption as results are not all kept in memory before being returned.
Instead of relying on `RETURN, PIPE ROW must be used to return the results, one row at a time.
Trying to return an expression from a pipelined function raises PLS-00633: RETURN statement in a pipelined function cannot contain an expression`
New lines and control characters can be injected in the source code by bad manipulations. Control characters aren’t visible to maintainers, so whether or not they are actually wanted should be double-checked. Note that this rule can optionally also report violations on literals containing the tabulation character.
Jump statements (`EXIT, CONTINUE, RETURN, RAISE, and RAISE_APPLICATION_ERROR), move control flow out of the current code block. So any statements that come after a jump are dead code.
This rule detects for statements that follow:
EXIT without a WHEN
CONTINUE without a WHEN
RETURN
RAISE
RAISE_APPLICATION_ERROR`
Labeled loops are useful, especially when the code is badly indented, to match the begin and end of each loop. Within a labeled loop, the code’s maintainability is increased by explicitly providing the loop’s label in every EXIT
statement. Indeed, if a nested loop is added afterwards, it is clear which loop has to be exited.
ASC or DESC should be specified for every column of an ORDER BY
clause to improve readability.
CREATE_TIMER
is generating network traffic each time the timer is fired. It’s probably totally fine for a timer being executed every hour but generally, this is used to provide clocks components that are going to generate network traffic every second or more.
It is recommended by Oracle to examine timers and replace them with JavaBeans.
Shadowing all exceptions with NULL statements indicates that no error handling has been done for a given block of code. This is a common bad-practice and only the non-relevant exceptions should be ignored (and a comment is welcome in such cases).
Declaring a `NUMBER variable without any precision wastes memory because Oracle supports up to 38 decimal digits by default (or the maximum supported by your system, whichever is less). If you don’t need that large a value, you should specify whatever matches your needs. This will save memory and provide extra integrity checking on input.
This rule also applies to some NUMBER subtypes as well: NUMERIC, DEC, and DECIMAL`.
Labeled loops are useful, especially when the code is badly indented, to match the begin and end of each loop. This rule verifies that loop start and end labels match, when both are specified.
The EXIT WHEN syntax can exit a loop depending on a condition. It should be preferred to the more verbose and error-prone IF … THEN EXIT; END IF;
syntax.
This rule applies whenever an `IF statement is followed by one or
more ELSEIF statements; the final ELSEIF should be followed by an ELSE statement.
The requirement for a final ELSE statement is defensive programming.
The ELSE statement should either take appropriate action or contain
a suitable comment as to why no action is taken. This is consistent with the
requirement to have a final ELSE clause in a CASE`
statement.
Not every data type supports the RANGE or scale constraints. Using these constraints on incompatible types results in an PLS-00572: improper constraint form used
exception being raised.
Assigning default values to variables in an initialization section is a good practice as it increases the readability of the code: when starting reading a block, one will know exactly which variables have default values.
When multiple tables are involved in a query, using table aliases helps to make it more understandable and keeps it short.
ORACLE 11g introduced the `SIMPLE_INTEGER data type, which is a sub-type of PLS_INTEGER, and covers the same range. There are three main differences between the two types:
SIMPLE_INTEGER is always NOT NULL. So when the value of the declared variable is never going to be null, you can declare it as SIMPLE_INTEGER.
You will never face a numeric overflow using SIMPLE_INTEGER because this data type wraps around without giving any error.
The SIMPLE_INTEGER data type gives a major performance boost over PLS_INTEGER when the code is compiled in “NATIVE” mode, because arithmetic operations on SIMPLE_INTEGER` type are performed directly at the hardware level.
The repetition of a prefix operator (+, -, or NOT
) is usually a typo. The second operator invalidates the first one:
Oracle’s ROWNUM is a pseudo column that numbers the rows in a result set. Unfortunately, it numbers the rows in the set before ordering is applied. So combining the two in the same query won’t get you the results you expect. Instead, you should move your selection and ordering into a subquery, and use ROWNUM
only on the outer query.
Labeled loops are useful, especially when the code is badly indented, to match the begin and end of each loop. This rule raises an issue when the end of a labeled loop is unlabeled.
Using the same name for multiple purposes reduces the understandability of the code and might eventually lead to bugs.
This rule verifies that no name is reused in an inner scope.
When data structures (scalar variables, collections, cursors) are declared in the package specification (not within any specific program), they can be referenced directly by any program running in a session with EXECUTE
rights to the package.
Instead, declare all package-level data in the package body and provide getter and setter functions in the package specification. Developers can then access the data using these methods and will automatically follow all rules you set upon data modification.
By doing so you can guarantee data integrity, change your data structure implementation, and also track access to those data structures.
SQL is an extremely powerful and hard to master language. It may be tempting to emulate SQL joins in PL/SQL using nested cursor loops, but those are not optimized by Oracle at all. In fact, they lead to numerous context switches between the SQL and PL/SQL engines, and those switches have a highly negative impact on performance. It is therefore much better to replace nested PL/SQL cursor loops with native SQL joins.
An EXCEPTION WHEN … THEN clause that only rethrows the caught exception has the same effect as omitting the EXCEPTION
clause altogether and letting it bubble up automatically.
Consistently using aliases for column names is useful for several reasons. The main one is that the code is independant from potential database modifications - when a column has been renamed to comply with standards for instance. Another reason is to remove ambiguity when querying several tables that may have equivalent column names.
For better readability, and to prevent the PLS-00312: a positional parameter association may not follow a named association
exception from being raised, do not mix named and positional argument invocations.
At most one declaration of a variable in a given scope is allowed in PL/SQL. The PLS-00371
error will be raised at runtime when attempting to reference a variable declared more than once.
The result_cache
Oracle hint can vastly improve performance, but it comes at the cost of extra memory consumption, so one should double-check that the gain in performance is significant, and avoid overusing this feature in general.
Compound triggers were introduced to ease the implementation of multiple triggers which need to work in cooperation.
Typically, a `FOR EACH ROW trigger accumulates facts, and an AFTER STATEMENT trigger performs the actual changes.
The compound trigger can hold a state common to all the triggers it defines, thereby removing the need to use package variables. This approach is sometimes the only possible one, as when avoiding a mutating table ORA-04091` error, or it can be used to get better performance.
However, there is no point in defining a compound trigger which contains only a single trigger, since there is no state to be shared. In such cases, a simple trigger should be used instead.
All reserved words should be written using the same case to ensure consistency in the code.
This rule checks that reserved words are all in lower case.
It is a best practice to have variable declarations outside of the loop. Additionally, declaring variables inside a loop is slightly less efficient because memory management is then performed with each iteration of the loop.
`%TYPE and %ROWTYPE seem like an easy way to make sure that a variable’s type always matches the type of the relevant column in an existing table. If the column type changes, then the variable changes with it.
However, Oracle Forms compiled against a procedure using either of these two symbols won’t get the benefit of that flexibility. Instead, at compile time, the relevant type is looked up from the underlying database and used in the form. If the column type changes later or the form is running against a database with different length semantics, attempting to use the form results in an “ORA-04062: Signature of package has been changed” error on the package in question. And the form needs to be recompiled on exactly the same database environment where it will run to avoid the error.
Note that %TYPE and %ROWTYPE` can be used in a package’s private procedures and functions, private package variables, and local variables without issue, but not in the package specification.
Labeled blocks are useful, especially when the code is badly indented, to help maintainers match the beginning and ending of each block. When blocks are nested, labeling them can improve the code’s readability. This rule detects nested block which do not have a start label.
Shared coding conventions allow teams to collaborate efficiently. This rule checks that all cursor names match the provided regular expression.
Calling COMMIT or ROLLBACK from within a trigger will lead to an ORA-04092
exception, unless the trigger has its own autonomous transaction.
Declaring a variable with the NOT NULL constraint incurs a small performance cost - while this constraint may not really be required. Using such a constraint should be avoided.
A FETCH … BULK COLLECT INTO without a LIMIT clause will load all the records returned by the cursor at once. This may lead to memory exhaustion. Instead, it is better to process the records in chunks using the LIMIT
clause.
Since Oracle 10g, DBMS_UTILITY.FORMAT_ERROR_BACKTRACE is available to get an exception’s stack trace, i.e. files and lines that lead up to the exception. When combined with DBMS_UTILITY.FORMAT_ERROR_STACK
, which contains the exception error code and message, developers are able quickly identify defects.
This rule verifies that whenever either is used in an exception handler, the other is used as well.
A GOTO
statement is an unstructured change in the control flow. They should be avoided and replaced by structured constructs.
Float data types, such as FLOAT, DOUBLE PRECISION, and REAL cannot have a scale constraint. Trying to specify a scale results in the exception PLS-00510: Float cannot have scale being raised.
Shared coding conventions allow teams to collaborate efficiently. This rule checks that reserved words are written in upper case.
When the FORALL statement is used without the SAVE EXCEPTIONS clause and an exception is raised by a DML query, the whole operation is rolled back and the exception goes unhandled. Instead of relying on this default behavior, it is better to always use the SAVE EXCEPTIONS clause and explicitly handle exceptions in a ORA-24381
handler.
Be careful about your use of Oracle-specific data types like `ROWID and UROWID. They might offer a slight improvement in performance over other means of identifying a single row (primary key or unique index value), but that is by no means guaranteed.
On the other hand, the use of ROWID or UROWID` means that your SQL statement will not be portable to other SQL databases. Further, many developers are not familiar with these data types, which can make the code harder to maintain.
Weak `REF CURSOR types are harder to work with than ones with a return type. Indeed, the compiler’s type-checker is unable to make some verifications, which are then delayed till runtime.
When the use of weak REF CURSOR is required, it is best to use the SYS_REFCURSOR built-in type instead of defining a new one.
This rule’s sysRefCursorAllowed parameter can be used to control whether or not the usage of SYS_REFCURSOR` is allowed.
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.
Trying to assign a large character value to a smaller variable or column will raise an error.
The CASE statement should be used only to clearly define some new branches in the control flow. As soon as a WHEN clause contains too many statements this highly decreases the readability of the overall control flow statement. In such case, the content of WHEN
clause should be extracted in a dedicated function.
From the Oracle docs:
Having all branches of a CASE or IF/ELSIF chain with the same implementation indicates a problem.
In the following code:
Explicit variable initializations with null values are superfluous, since unassigned variables are implicitly initialized to null.
Labeled blocks are useful to help maintainers match-up the beginning and ending of each section of code, especially when that code is badly indented. However, if used, those labels must appear on the same line as the “END” keyword in order to avoid confusion. Otherwise, the label might be misread by maintainers as a procedure call.
Currently, VARCHAR and VARCHAR2 are identical data types. But to prevent future changes in behavior, Oracle recommends the use of VARCHAR2.
NCHAR and NVARCHAR2 lengths must be given in characters, not bytes. This is partly because a single character may occupy more than a single byte in memory. Specify the field length in bytes, and theoretically your value could overrun the field, but instead Oracle simply refuses to run the code. Specify it in characters, and Oracle will allocate the appropriate number of bytes to store the requested number of characters. Trying to specify the length semantics in bytes will result in the PLS-00639: NCHAR/NVARCHAR2 cannot be byte length semantics
exception being raised.
Always having a RETURN as the last statement in a function is a good practice as it prevents the ORA-06503 PL/SQL: Function returned without value
error.
Centralizing the definitions of custom exceptions comes with two major benefits:
The duplication of the exceptions declarations and
PRAGMA EXCEPTION_INIT
is avoidedThe risk of associating multiple different exceptions to the same number is reduced
Using a `NUMBER to store an integer is less performant than using a PLS_INTEGER. PLS_INTEGERs require less storage than NUMBERs, and benefit from the use of hardware math, as opposed to the library math required for NUMBERs. Even more performant is the SIMPLE_INTEGER subtype of PLS_INTEGER. However, changing to either of these types is only appropriate under certain circumstances.
PLS_INTEGER is only a candidate for NUMBER with a scale of up to 9.
SIMPLE_INTEGER has the same size limitation, in addition to it’s NOT NULL constraint and lack of overflow checking.
This rule raises an issue when a NUMBER` is declared with a scale of 9 or less.
Simple loops, of the form LOOP … END LOOP
, behave by default as infinite ones, since they do not have a loop condition. They can often be replaced by other, safer, loop constructs.
Labels are useful to match the begin and end of each PACKAGE, PROCEDURE or FUNCTION, especially when the code is badly indented or too much nested.
This rule raised an issue when the END statement of a PACKAGE, PROCEDURE or FUNCTION is having no label matching the name of the corresponding “begin” statement.
`UNION is a convenient syntax to combine the results of two or more SQL statements because it helps you cut a complex problem into multiple simple SQL statements. But when it comes to execution, using UNION is debatable.
First, it may be possible to fuse two simple SQL statements into a bigger one that will run faster. Second, UNION is significantly less performant compared to UNION ALL because it removes duplicated entries and runS an internal DISTINCT to achieve this.
UNION ALL does not remove duplicates and returns all the results from the queries. It performs faster in most cases compared to UNION. Nevertheless, the quantity of data returned by UNION ALL can be significantly larger than with UNION. On a slow network, the performance gain of using UNION ALL instead of UNION can be negated by the time lost in the larger data transfer.
This rule raises an issue on each UNION. It’s up to the developer to challenge its use and see if there is a better way to rewrite without UNION`.
IF statements with conditions that are always false have the effect of making blocks of code non-functional. This can be useful during debugging, but should not be checked in. IF statements with conditions that are always true are completely redundant, and make the code less readable. In either case, unconditional IF
statements should be removed.
Jumping back to a previous statement using GOTO
is a way to reimplement loops, which PL/SQL already provides in much more readable forms.
RAISE_APPLICATION_ERROR may only be called with an error code from -20,000 to -20,999, which is the range reserved for application errors. When called with another value, Oracle raises the exception: ORA-21000: error number argument to raise_application_error of 0 is out of range.
The deprecated MLSLABEL datatype is still supported only for backwards compatibility with Trusted Oracle, and since Oracle8, the only valid value it can hold is NULL. Thus, the usage of this type should be progressively removed.
For fixed-length values, a `CHAR field occupies the same amount of disk space as a VARCHAR2 field, but for variable-length values CHAR fields use more storage space and make searching more difficult by right-padding values with whitespaces. Therefore VARCHAR2 fields are preferred. Similarly, NCHAR should be replaced by NVARCHAR2.
Note that for 1-character fields, CHAR is naturally equivalent to VARCHAR2`, but the latter is still preferred for consistency.
Using the same name for multiple purposes reduces the understandability of the code and might eventually lead to bugs.
This rule verifies that no label is reused in an inner scope.
A CROSS JOIN
query will return all records where each row from the first table is combined with each row from the second table. This means that such a query returns the Cartesian product of the sets of rows from the joined tables, which is why it is also know as “Cartesian product query”.
Such a query can return a huge amount of data, and therefore should be used only with great caution and only when really needed.
Because RESULT_CACHE
-enabled functions increase memory consumption, one should double-check that the gain in performances is significant, and avoid over-using this feature in general.
Hard-coding bounds in FOR loops is a bad practice, just as magic numbers in general are. Often, those magic bounds can be replaced by dynamic values. If that is not possible, replacing the literal number with a constant is still better.
Oracle supports at most 1000 items in a SQL query’s IN clause. When more items are given, the exception ORA-01795 maximum number of expressions in a list is 1000 is raised. Thus, IN
clauses are not as scalable as joins.
The performance of DML queries in loops can be improved by placing them in a FORALL
statement. This way, queries will be sent in bulk, minimizing the number of context switches between PL/SQL and SQL.
Unused cursor parameters are misleading. Whatever the values passed to such parameters, the behavior will be the same.
In a CREATE PACKAGE BODY, the purpose of the initialization section is to set the initial values of the package’s global variables. It is therefore surprising to find a RETURN statement there, as all its following statements will be unreachable.
The `FOR loop at first seems like a convenient way of iterating over the elements of a collection, but doing so will raise a VALUE_ERROR exception if the collection is empty. Looping instead from 1 to COUNT doesn’t work either if the collection is sparse; that leads to a ORA-01403: no data found error.
Instead, a WHILE` loop should be used.
The requirement for a final ELSE clause is defensive programming. The CASE
statement should always provide a value.
Quoted identifiers are confusing to many programmers, as they look similar to string literals. Moreover, for maximum portability, identifiers should be self-descriptive and should not contain accents. Quoted identifiers can contain any character, which can be confusing.
Using FORALL i IN x.first … x.last or FORALL i IN 1 … x.count might fail when indexed collections are sparse as Oracle tries to access non-existent element(s) of x. FORALL i IN INDICES OF x syntax will always work including sparse collections. Thus using FORALL i IN INDICES OF x
should be preferred as it makes code more robust and easier to review.
It is needlessly complex to invert the result of a boolean comparison. The opposite comparison should be made instead.
Trailing whitespaces bring no information, they may generate noise when comparing different versions of the same file, and they can create bugs when they appear after a \ marking a line continuation. They should be systematically removed.
An automated code formatter allows to completely avoid this family of issues and should be used wherever possible.
Putting multiple statements on a single line lowers the code readability and makes debugging the code more complex.
Unresolved directive in <stdin> - include::{noncompliant}[]
Write one statement per line to improve readability.
Unresolved directive in <stdin> - include::{compliant}[]
When a SQL query is joining n tables (with n>=2), it is expected to have join conditions defined to determine on which columns these n tables should be joined. At minimum, for n joined tables, the SQL query should contain (n-1) join conditions involving all the joined table to avoid a full cartesian product between the rows of the n tables.
Not doing so will imply that too many rows will be returned. If this is not the case and unless this has been done on purpose, the SQL query should be reviewed and missing conditions should be added or useless tables should be removed from the SQL query.
This rule is raising no issue when the SQL query is involving CROSS JOIN, NATURAL JOIN
statements.
When handling a caught exception, the original exception’s message and stack trace should be logged or passed forward.
Allowing an application to dynamically change the structure of a database at runtime is very dangerous because the application can become unstable under unexpected conditions. Best practices dictate that applications only manipulate data.
Magic numbers make the code more complex to understand as it requires the reader to have knowledge about the global context to understand the number itself. Their usage may seem obvious when writing the code, but it may not be the case for another developer or later once the context faded away. -1, 0, and 1 are not considered magic numbers.
A boolean literal can be represented in two different ways: {true} or {false}. They can be combined with logical operators ({ops}) to produce logical expressions that represent truth values. However, comparing a boolean literal to a variable or expression that evaluates to a boolean value is unnecessary and can make the code harder to read and understand. The more complex a boolean expression is, the harder it will be for developers to understand its meaning and expected behavior, and it will favour the introduction of new bugs.
When exceptions occur, it is usually a bad idea to simply ignore them. Instead, it is better to handle them properly, or at least to log them.
SQL queries that use EXISTS subqueries are inefficient because the subquery is re-run for every row in the outer query’s table. There are more efficient ways to write most queries, ways that do not use the EXISTS
condition.
Nested code - blocks of code inside blocks of code - is eventually necessary, but increases complexity. This is why keeping the code as flat as possible, by avoiding unnecessary nesting, is considered a good practice.
Merging if statements when possible will decrease the nesting of the code and improve its readability.
Each source file should start with a header stating file ownership and the license which must be used to distribute the application.
This rule must be fed with the header text that is expected at the beginning of every file.
The use of parentheses, even those not required to enforce a desired order of operations, can clarify the intent behind a piece of code. However, redundant pairs of parentheses could be misleading and should be removed.
UPDATE and DELETE statements should contain WHERE
clauses to keep the modification of records under control. Otherwise unexpected data loss could result.
A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body. While this might seem harmless at first glance, it can lead to confusion and potential errors in your code. Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore. Therefore, removing function parameters that are not being utilized is considered best practice.
Shared naming conventions improve readability and allow teams to collaborate efficiently. This rule checks that all package names match a provided regular expression.
There is no reason to re-assign a variable to itself. Either this statement is redundant and should be removed, or the re-assignment is a mistake and some other value or variable was intended for the assignment instead.
Executing code dynamically is security-sensitive. It has led in the past to the following vulnerabilities:
Some APIs enable the execution of dynamic code by providing it as strings at runtime. These APIs might be useful in some very specific meta-programming use-cases. However most of the time their use is frowned upon because they also increase the risk of maliciously Injected Code. Such attacks can either run on the server or in the client (example: XSS attack) and have a huge impact on an application’s security.
This rule marks for review each occurrence of such dynamic code execution. This rule does not detect code injections. It only highlights the use of APIs which should be used sparingly and very carefully.
Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.
SELECT *
should be avoided because it releases control of the returned columns and could therefore lead to errors and potentially to performance issues.
According to the US National Institute of Standards and Technology (NIST), the Data Encryption Standard (DES) is no longer considered secure:
For similar reasons, RC2 should also be avoided.
Marking a parameter for output means that callers will expect its value to be updated with a result from the execution of the procedure. Failing to update the parameter before the procedure returns is surely an error.
The MD5 algorithm and its successor, SHA-1, are no longer considered secure, because it is too easy to create hash collisions with them. That is, it takes too little computational effort to come up with a different input that produces the same MD5 or SHA-1 hash, and using the new, same-hash value gives an attacker the same access as if he had the originally-hashed value. This applies as well to the other Message-Digest algorithms: MD2, MD4, MD6, HAVAL-128, HMAC-MD5, DSA (which uses SHA-1), RIPEMD, RIPEMD-128, RIPEMD-160, HMACRIPEMD160.
Consider using safer alternatives, such as SHA-256, SHA-512 or SHA-3.
This rule verifies that single-line comments are not located at the ends of lines of code. The main idea behind this rule is that in order to be really readable, trailing comments would have to be properly written and formatted (correct alignment, no interference with the visual structure of the code, not too long to be visible) but most often, automatic code formatters would not handle this correctly: the code would end up less readable. Comments are far better placed on the previous empty line of code, where they will always be visible and properly formatted.
Shared coding conventions allow teams to collaborate efficiently. This rule checks that all function names match a provided regular expression.
Overriding a variable declared in an outer scope can strongly impact the readability, and therefore the maintainability, of a piece of code. Further, it could lead maintainers to introduce bugs because they think they’re using one variable but are really using another.
Tables without primary keys are largely unusable in a relational database because they cannot be joined to. A primary key should be specified at table creation to guarantee that all its records have primary key values.
Return of boolean literal statements wrapped into if-then-else
ones should be simplified.
Even though the ORDER BY
clause supports using column numbers, doing so makes the code difficult to read and maintain. Therefore the use of column names is preferred.
An unused local variable is a variable that has been declared but is not used anywhere in the block of code where it is defined. It is dead code, contributing to unnecessary complexity and leading to confusion when reading the code. Therefore, it should be removed from your code to maintain clarity and efficiency.
Dead stores refer to assignments made to local variables that are subsequently never used or immediately overwritten. Such assignments are unnecessary and don’t contribute to the functionality or clarity of the code. They may even negatively impact performance. Removing them enhances code cleanliness and readability. Even if the unnecessary operations do not do any harm in terms of the program’s correctness, they are - at best - a waste of computing resources.
A naming convention in software development is a set of guidelines for naming code elements like variables, functions, and classes. {identifier_capital_plural} hold the meaning of the written code. Their names should be meaningful and follow a consistent and easily recognizable pattern. Adhering to a consistent naming convention helps to make the code more readable and understandable, which makes it easier to maintain and debug. It also ensures consistency in the code, especially when multiple developers are working on the same project.
This rule checks that {identifier} names match a provided regular expression.
Variables should not be shadowed
Having two branches in an IF/ELSIF chain with the same implementation is at best duplicate code, and at worst a coding error.
PL/SQL does not support nested C-style (
/* … */
) comments.