Skip to content

Tsql Rules ​

This section contains 5 rule(s) related to tsql.

Rule Summary ​

CodeNameAliasesFix Compatible
TQ01tsql.sp_prefixL056❌
TQ02tsql.procedure_begin_end-✅
TQ03tsql.empty_batch-✅
TQ04tsql.prefer_as_alias-✅
TQ05tsql.if_while_begin_end-✅

TQ01: tsql.sp_prefix ​

SP_ prefix should not be used for user-defined stored procedures in T-SQL.

PropertyValue
Nametsql.sp_prefix
AliasesL056
Groupsall, tsql
Auto-fixableNo ❌

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.

sql
CREATE PROCEDURE dbo.sp_pull_data
AS
SELECT
    ID,
    DataDate,
    CaseOutput
FROM table1

Best practice

Use a different name for the stored procedure.

sql
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 table1

TQ02: tsql.procedure_begin_end ​

Procedure bodies with multiple statements should be wrapped in BEGIN/END.

PropertyValue
Nametsql.procedure_begin_end
Groupsall, tsql
Auto-fixableYes ✅

Anti-pattern

Procedure bodies with multiple statements should be wrapped in BEGIN/END for clarity and consistency.

sql
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.

sql
CREATE PROCEDURE Reporting.MultipleStatements
AS
BEGIN
    SELECT
        [ID],
        [DataDate],
        [CaseOutput]
    FROM Table1;

    SELECT
        [ID],
        [DataDate],
        [CaseOutput]
    FROM Table2;
END

TQ03: tsql.empty_batch ​

Remove empty batches.

PropertyValue
Nametsql.empty_batch
Groupsall, tsql
Auto-fixableYes ✅

Anti-pattern

Empty batches (containing only GO statements) should be removed.

sql
CREATE TABLE dbo.test (
    testcol1 INT NOT NULL,
    testcol2 INT NOT NULL
);

GO

GO

Best practice

Remove empty batches.

sql
CREATE TABLE dbo.test (
    testcol1 INT NOT NULL,
    testcol2 INT NOT NULL
);

GO

TQ04: tsql.prefer_as_alias ​

Prefer ANSI-style AS aliasing over alias = expression in T-SQL.

PropertyValue
Nametsql.prefer_as_alias
Groupsall, tsql
Auto-fixableYes ✅

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

sql
SELECT
    help3 = 'hello',
    help4 = CASE WHEN help = 'apple' THEN 'hello' END

Best practice

sql
SELECT
    'hello' AS help3,
    CASE WHEN help = 'apple' THEN 'hello' END AS help4

Configuration

OptionDescription
force_enableRun 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.

PropertyValue
Nametsql.if_while_begin_end
Groupsall, tsql
Auto-fixableYes ✅

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

sql
IF @flag = 1
    SELECT * FROM Table1;

SELECT * FROM Table2;

Best practice

Wrap each body in BEGIN/END.

sql
IF @flag = 1
BEGIN
    SELECT * FROM Table1;
END

SELECT * FROM Table2;

Configuration

OptionDescription
force_enableRun this rule even for dialects where this rule is disabled by default. Must be one of [True, False].

Released under the MIT License.