Tsql Rules
This section contains 5 rule(s) related to tsql.
Rule Summary
| Code | Name | Aliases | Fix Compatible |
|---|---|---|---|
| TQ01 | tsql.sp_prefix | L056 | ❌ |
| TQ02 | tsql.procedure_begin_end | - | ✅ |
| TQ03 | tsql.empty_batch | - | ✅ |
| TQ04 | tsql.prefer_as_alias | - | ✅ |
| TQ05 | tsql.if_while_begin_end | - | ✅ |
TQ01: tsql.sp_prefix
SP_ prefix should not be used for user-defined stored procedures in T-SQL.
| Property | Value |
|---|---|
| Name | tsql.sp_prefix |
| Aliases | L056 |
| Groups | all, tsql |
| Auto-fixable | No ❌ |
Anti-pattern
The SP_ prefix is used to identify system procedures and can adversely affect performance of the user-defined stored procedure. It can also break system procedures if there is a naming conflict.
CREATE PROCEDURE dbo.sp_pull_data
AS
SELECT
ID,
DataDate,
CaseOutput
FROM table1Best practice
Use a different name for the stored procedure.
CREATE PROCEDURE dbo.pull_data
AS
SELECT
ID,
DataDate,
CaseOutput
FROM table1
-- Alternatively prefix with USP_ to
-- indicate a user-defined stored procedure.
CREATE PROCEDURE dbo.usp_pull_data
AS
SELECT
ID,
DataDate,
CaseOutput
FROM table1TQ02: tsql.procedure_begin_end
Procedure bodies with multiple statements should be wrapped in BEGIN/END.
| Property | Value |
|---|---|
| Name | tsql.procedure_begin_end |
| Groups | all, tsql |
| Auto-fixable | Yes ✅ |
Anti-pattern
Procedure bodies with multiple statements should be wrapped in BEGIN/END for clarity and consistency.
CREATE PROCEDURE Reporting.MultipleStatements
AS
SELECT
[ID],
[DataDate],
[CaseOutput]
FROM Table1;
SELECT
[ID],
[DataDate],
[CaseOutput]
FROM Table2;Best practice
Wrap procedure bodies with multiple statements in BEGIN/END blocks.
CREATE PROCEDURE Reporting.MultipleStatements
AS
BEGIN
SELECT
[ID],
[DataDate],
[CaseOutput]
FROM Table1;
SELECT
[ID],
[DataDate],
[CaseOutput]
FROM Table2;
ENDTQ03: tsql.empty_batch
Remove empty batches.
| Property | Value |
|---|---|
| Name | tsql.empty_batch |
| Groups | all, tsql |
| Auto-fixable | Yes ✅ |
Anti-pattern
Empty batches (containing only GO statements) should be removed.
CREATE TABLE dbo.test (
testcol1 INT NOT NULL,
testcol2 INT NOT NULL
);
GO
GOBest practice
Remove empty batches.
CREATE TABLE dbo.test (
testcol1 INT NOT NULL,
testcol2 INT NOT NULL
);
GOTQ04: tsql.prefer_as_alias
Prefer ANSI-style AS aliasing over alias = expression in T-SQL.
| Property | Value |
|---|---|
| Name | tsql.prefer_as_alias |
| Groups | all, tsql |
| Auto-fixable | Yes ✅ |
T-SQL supports an alternative alias form in SELECT clauses using alias = expression. This rule enforces the more ANSI-style expression AS alias form instead.
This rule only applies to the tsql dialect and is disabled by default. Enable it with the force_enable = True flag.
Anti-pattern
SELECT
help3 = 'hello',
help4 = CASE WHEN help = 'apple' THEN 'hello' ENDBest practice
SELECT
'hello' AS help3,
CASE WHEN help = 'apple' THEN 'hello' END AS help4Configuration
| Option | Description |
|---|---|
force_enable | Run this rule even for dialects where this rule is disabled by default. Must be one of [True, False]. |
TQ05: tsql.if_while_begin_end
IF, ELSE and WHILE bodies should be wrapped in BEGIN/END.
| Property | Value |
|---|---|
| Name | tsql.if_while_begin_end |
| Groups | all, tsql |
| Auto-fixable | Yes ✅ |
Without BEGIN/END only the first statement after the condition belongs to the IF, ELSE or WHILE. Later statements always run, which is easy to miss when reading or editing the code.
This rule only applies to the tsql dialect and is disabled by default. Enable it with the force_enable = True flag.
Anti-pattern
IF @flag = 1
SELECT * FROM Table1;
SELECT * FROM Table2;Best practice
Wrap each body in BEGIN/END.
IF @flag = 1
BEGIN
SELECT * FROM Table1;
END
SELECT * FROM Table2;Configuration
| Option | Description |
|---|---|
force_enable | Run this rule even for dialects where this rule is disabled by default. Must be one of [True, False]. |
