Code Inspections in SQL
This topic lists all GoLand code inspections available in SQL.
You can toggle specific inspections or change their severity level on the Editor | Inspections page of the IDE settings CtrlAlt0S.
Inspection | Description | Default Severity |
---|---|---|
Adding not null column without default value | Reports attempts to create NOT NULL columns without DEFAULT values. Example (Microsoft SQL Server):
By default, a column holds NULL values. In the example, we use the NOT NULL constraint that enforces a column not to accept NULL values. If we prohibit to use NULL values, we must set the DEFAULT value that SQL can use when we create a new record.
You can quickly add the DEFAULT value by using the Add DEFAULT value quick-fix. | |
Aggregate-related problems | Reports invalid usages of SQL aggregate functions. The following situations are considered:
| |
Ambiguous reference | Reports columns that have identical names but belong to different tables. Example (MySQL):
The
| |
Auto-increment duplicate | Reports tables that contain two columns with an automatic increment. In MySQL, Microsoft SQL Server, and Db2 dialects, a table can have only one field with a auto-increment option, and this field must be a key. Example (MySQL):
The AUTO_INCREMENT constraint for
| |
Check using clause columns | Reports columns in the USING clause that does not exist in both tables. Example (MySQL):
In USING clauses, a column name must be present in both tables, and the SELECT query will automatically join those tables by using the given column name. As we do not have the
| |
Column is shadowed by alias | Reports SELECT aliases with names that match column names in the FROM clause. Example (MySQL):
The | |
Column should be in group by clause | Reports columns that are not in the GROUP BY clause or inside an aggregate function call. Example (Microsoft SQL Server):
If you run the SELECT query, you will receive an error because Microsoft SQL Server expects the
| |
Constant condition | Reports conditions in WHERE or JOIN clauses that are always TRUE or always FALSE. Example (MySQL):
The | |
Constant expression | Reports conditions and expressions that are always true, false or null. Example (MySQL):
The The | |
Current console schema introspected | Reports schemas and databases in the current session that are not introspected. For example, this warning might occur when you try to create a table in the schema that is not introspected. Introspection is a method of inspecting a data source. When you perform introspection, structural information in the data source is inspected to detect tables, columns, functions, and other elements with their attributes. | |
Delete or update statement without where clauses | Reports usages of DELETE or UPDATE statements without WHERE clauses. Without WHERE clauses, DELETE drops all the data from the table, and UPDATE overwrites values for all the table rows. Example (MySQL):
| |
Deprecated type | Reports usages of types that are deprecated and might disappear in future versions of DBMS. Reported types:
Example (Oracle):
| |
Duplicating column name in SELECT | Reports duplicated names of column aliases in SELECT lists. Example (Sybase ASE):
The | |
Each derived table should have alias | Reports derived tables without aliases. Example (MySQL):
According to Derived Tables at dev.mysql.com, an alias is mandatory. You can add the alias by using the Introduce alias quick-fix. After the quick-fix is applied:
| |
Function signature | Reports signature issues for built-in functions. The inspection will report a wrong number of arguments, invalid keywords, wrong data types, and other issues. Example (MySQL):
In MySQL, the | |
Identifier should be quoted | Reports situations when you use SQL reserved keywords as identifier names in your query. Example (Microsoft SQL Server):
We use After the quick-fix is applied:
| |
Ill-formed date/time literals | Reports errors in date and time literals. This inspection is available in MySQL, Oracle, Db2, and H2. Example (MySQL):
In this example, dates ignore the MySQL standard for date and time literals. Therefore, they will be highlighted. For more information about date and time literals in MySQL, see Date and Time Literals at dev.mysql.com. The following date and type literals are valid for MySQL.
| |
Illegal cursor state | Reports illegal cursor states inside SQL routines.
Example (Microsoft SQL Server):
According to CLOSE (Transact-SQL) at docs.microsoft.com, CLOSE must be issued on an open cursor, and CLOSE is not allowed on cursors that have only been declared or are already closed. So, we need to open the cursor to fix the warning.
| |
Implicit string truncation | Reports variables that exceed the defined length in characters. Example (Microsoft SQL Server):
The After the quick-fix is applied:
| |
Index is dependent on column | Reports cases when you try to drop columns from indexed tables. This inspection is available in Microsoft SQL Server and Sybase ASE. Example (Microsoft SQL Server):
You cannot delete the | |
Insert NULL into NOT NULL column | Reports cases when you insert NULL values into columns that accept only NOT NULL values. Example (Microsoft SQL Server):
You cannot insert NULL values in
| |
Insertion into generated columns | Reports INSERT statements that assign values to generated columns. Generated columns can be read, but their values can not be directly written. Example (PostgreSQL):
You cannot insert | |
Misleading references | Reports ambiguous references in SQL code. For example, when a name refer to both a table column and a routine parameter. The execution of such code might lead to errors or unexpected results due to counter-intuitive resolution logic. Usually, names with a more local scope have higher priority. Example (PostgreSQL):
In PostgreSQL, you can use the
| |
Missing column aliases | Reports queries without explicit aliases in output expressions (for example, in the SELECT statement). Example (PostgreSQL):
| |
Missing return statement | Reports functions that have no RETURN statements. Example (Oracle):
The
| |
Multiple row limiting/offset clauses in queries | Reports usages of multiple row limiting clauses in a single query. Example (Microsoft SQL Server):
The SELECT TOP clause is used to specify that only 1 record must be returned. The FETCH clause specifies the number of rows to return after the OFFSET clause has been processed. But as we already have the SELECT TOP limiting clause, the FETCH clause might be redundant. | |
Named arguments should be used | Reports arguments that are used without names in routine calls. By default, this inspection is disabled. For more information about the difference between named and unnamed parameters, see Binding Parameters by Name (Named Parameters) at docs.microsoft.com. Example (Microsoft SQL Server):
Parameters | |
No data sources configured | Reports the absence of data sources in the Database tool window (View | Tool Windows | Database). | |
Null comparison | Reports comparisons with NULL that can be replaced with IS NULL or IS NOT NULL operators. Example (Microsoft SQL Server):
The
| |
Redundant ELSE NULL clause | Reports redundant ELSE NULL clauses. Example (MySQL):
The | |
Redundant alias expressions | Reports alias expressions that duplicate names of columns in tables and might be redundant. Example (PostgreSQL):
The first two aliases use the same column names as in the | |
Redundant code in COALESCE call | Reports all the arguments except for the first expression that does not evaluate to NULL in COALESCE functions. Example (MySQL):
The first NOT NULL argument is | |
Redundant ordering direction | Reports redundant ordering directions like ASC and DESC in ORDER BY clauses. Example (MySQL):
The ORDER BY keyword sorts the records in the ascending order by default. So, the | |
Redundant row limiting in queries | Reports redundant row limiting clauses like FETCH and LIMIT in queries. Example (PostgreSQL):
To fix the warning, you can add OFFSET to limiting clauses. If OFFSET is missing, then LIMIT is redundant because the usage of LIMIT does not influence the operation result of EXISTS. In case with OFFSET, we skip first
| |
SQL dialect detection | Reports situations when a dialect is not assigned to an SQL file. For example, when you open a new SQL file without assigning a dialect to it, you see a notification where the best matching dialect is advised. Click the Use <dialect> link to use the advised dialect. Alternatively, click the Change dialect to link to select the other dialect. | |
SQL source modification detection | Reports situations when source code of a database object has been changed. The inspection is triggered when you perform database or object introspection. The introspection is run when you open source code of an object, run statements, and perform code refactoring. Also, you can run introspection by right-clicking an object and selecting Refresh. The inspection covers the following situations:
| |
Statement with side effects | Reports statements that might lead to modification of a database during a read-only connection. To enable read-only mode for a connection, right-click a data source in the Database tool window (View | Tool Windows | Database) and select Properties. In the Data Sources and Drivers dialog, click the Options tab and select the Read-only checkbox. Example (MySQL):
As | |
Suspicious code in triggers | Reports incorrect usages of transition table variables in triggers. Example (HSQLDB):
In HSQLDB, DELETE triggers may be used only with the OLD state while INSERT triggers may have only the NEW state. So, in the previous example, NEW in | |
Types compatibility | Reports type-related errors. | |
Unicode usage in SQL | Reports string literals that use national characters without the Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters. For more information, see nchar and nvarchar (Transact-SQL) at docs.microsoft.com. Example (Microsoft SQL Server):
The | |
Unreachable code | Reports unreachable statements inside SQL routines. Example (Microsoft SQL Server):
In Microsoft SQL Server, the | |
Unresolved reference | Reports unresolved SQL references. Example (MySQL):
The | |
Unsafe 'join' clause in 'delete' statement | Reports missing conditional checks for statements that might modify the whole database. For example, usages of JOIN clauses inside DELETE statements without ON or WHERE. Without conditional checks on JOIN, DELETE drops contents of the entire table. Example (MySQL):
| |
Unused common table expression | Reports unused common table expressions (CTE) inside the query. Example (PostgreSQL):
By using WITH, we create a temporary named result set with the name | |
Unused subquery item | Reports columns, aliases, and other subquery items that are not referenced in the outer query expression. Example (PostgreSQL):
We reference | |
Unused variable | Reports unused arguments, variables, or parameters. Example (PostgreSQL):
The | |
Usages of GOTO statements | Reports usages of backward GOTO statements and GOTO statements used to exit a loop. The extensive use of GOTO statements is generally not recommended. For details, see GOTO statement in SQL procedures at ibm.com. Instead of jumping back to a previous statement using GOTO, consider using a loop. Instead of exiting the WHILE loop with GOTO, consider using other control-of-flow statements (for example, RETURN or BREAK). Example (Oracle):
| |
Use of transaction management statements in triggers | Reports usages of transaction management statements like COMMIT or ROLLBACK in trigger bodies. With COMMIT or ROLLBACK statements in a trigger body, the trigger will not compile. The fail happens because triggers start during transactions. When the trigger starts the current transaction is still not complete. As COMMIT terminates a transaction, both statements (COMMIT and ROLLBACK) would lead to an exception. Changes that are executed in a trigger should be committed (or rolled back) by the owning transaction that started the trigger. Example (Oracle):
| |
Using CASE instead of COALESCE function and vice versa | Reports situations when CASE and COALESCE calls are interchangeable. This inspection has the following intention actions: Replace with 'COALESCE' call and the opposite one Replace with CASE expression. Example (MySQL):
In the example, the CASE statement can be replaced with If you prefer using CASE expressions, select the Prefer CASE expressions over COALESCE function option on the inspection page. | |
Using CASE instead of conditional function and vice versa | Reports situations when CASE and IF are interchangeable. Example (MySQL):
To keep your code short, you can replace the CASE structure with IF. You can do that by applying the Replace with 'IF' call intention action. The example code will look as follows:
To revert IF to CASE, click IF and apply the Replace with CASE expression intention action. | |
Using of named and positional arguments | Reports calls in which positional arguments go after the named ones. Works in PostgreSQL, Oracle, and Db2. Example (In PostgreSQL):
| |
VALUES clause cardinality | Reports situations when a number of parameters in VALUES does not match a number of columns in a target table. Example (MySQL):
The | |
Thanks for your feedback!