Writing SQL code is not just about making it work — it’s also about making it understandable, maintainable, and audit-friendly. Proper commenting is a critical part of writing professional SQL Code. In this post, we’ll explore the benefits of SQL comments, standard commenting practices, and examples for different SQL objects, including tables, procedures, functions, triggers, sequences, and indexes.

Why Comment in SQL Code?

Commenting SQL code brings several benefits:

  1. Improves Readability: Helps developers understand your code without digging into every line.

  2. Facilitates Maintenance: Makes it easier to fix bugs or enhance features later.

  3. Audit & Documentation: Useful in enterprise environments for tracking who created or modified objects.

  4. Reduces Human Error: Helps teams follow standards and avoid mistakes.

  5. Supports Collaboration: Makes it easier for multiple developers to work on the same database.

Commenting Standards

A good SQL commenting standard should include:

Effective Commenting for Tables, Functions, Triggers, Indexes, and Sequences

1. Tables

/******************************************************************************************
* Table Name   : dbo.Roles
* Description  : Stores system roles with audit columns.
* Author       : Raj Bhatt
* Created On   : 2025-10-09
* Last Modified: 2025-10-09
* Notes:
*   - Includes CreatedBy, CreatedDate, UpdatedBy, UpdatedDate columns for auditing.
******************************************************************************************/

CREATE TABLE dbo.Roles (
    RoleId INT IDENTITY(1,1) PRIMARY KEY,           -- Unique identifier for the role
    RoleName VARCHAR(50) NOT NULL,                  -- Name of the role
    Description VARCHAR(255) NULL,                  -- Optional description of the role
    CreatedBy VARCHAR(100) NULL,                    -- User who created the record
    CreatedDate DATETIME DEFAULT GETDATE(),         -- Timestamp when record was created
    UpdatedBy VARCHAR(100) NULL,                    -- User who last updated the record
    UpdatedDate DATETIME NULL                        -- Timestamp when record was last updated
);

Important points

2. Functions

/******************************************************************************************
* Function Name : fn_GetRoleName
* Description   : Returns the RoleName for a given RoleId.
* Author        : Raj Bhatt
* Created On    : 2025-10-09
******************************************************************************************/
CREATE FUNCTION dbo.fn_GetRoleName (@RoleId INT)
RETURNS VARCHAR(50)
AS
BEGIN
    DECLARE @RoleName VARCHAR(50);

    -- Fetch RoleName from Roles table
    SELECT @RoleName = RoleName
    FROM dbo.Roles
    WHERE RoleId = @RoleId;

    RETURN @RoleName;
END;
GO

Always think about where and how the function will be used. If it’s called millions of times in a query, performance optimization is critical

3. Triggers

/******************************************************************************************
* Trigger Name : trg_Update_Roles
* Table Name   : dbo.Roles
* Description  : Automatically updates UpdatedDate when a record in Roles is modified.
* Author       : Raj Bhatt
* Created On   : 2025-10-09
* Last Modified: 2025-10-09
* Notes:
*   - UpdatedBy must be set manually.
*   - Ensures audit consistency across updates.
******************************************************************************************/

CREATE TRIGGER trg_Update_Roles
ON dbo.Roles
AFTER UPDATE
AS
BEGIN
    SET NOCOUNT ON;  -- Prevent "rows affected" messages for better performance

    BEGIN TRY
        -- Update the UpdatedDate to current timestamp for all modified rows
        UPDATE r
        SET r.UpdatedDate = GETDATE()
        FROM dbo.Roles AS r
        INNER JOIN inserted AS i ON r.RoleId = i.RoleId;
    END TRY
    BEGIN CATCH
        -- Error handling: raise meaningful error message
        DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
        DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
        DECLARE @ErrorState INT = ERROR_STATE();

        RAISERROR('Error in trigger trg_Update_Roles: %s', @ErrorSeverity, @ErrorState, @ErrorMessage);
    END CATCH;
END;
GO

4. Sequences

/******************************************************************************************
* Sequence Name : seq_OrderId
* Description   : Generates unique OrderId for Orders table.
* Author        : Raj Bhatt
* Created On    : 2025-10-09
******************************************************************************************/
CREATE SEQUENCE dbo.seq_OrderId
    START WITH 1
    INCREMENT BY 1;

5. Indexes

/******************************************************************************************
* Index Name   : IX_Roles_RoleName
* Table Name   : dbo.Roles
* Description  : Non-clustered index on RoleName for faster search.
* Author       : Raj Bhatt
* Created On   : 2025-10-09
******************************************************************************************/
CREATE NONCLUSTERED INDEX IX_Roles_RoleName
ON dbo.Roles (RoleName);

6. Stored Procedures

/******************************************************************************************
* Procedure Name : sp_GetRoleById
* Description    : Retrieves role details by RoleId.
* Author         : Raj Bhatt
* Created On     : 2025-10-09
* Last Modified  : 2025-10-09
* Parameters:
*   @RoleId INT - Role identifier
* Returns:
*   Role details from dbo.Roles
******************************************************************************************/
CREATE PROCEDURE dbo.sp_GetRoleById
    @RoleId INT
AS
BEGIN
    SET NOCOUNT ON;  -- Prevent "rows affected" messages

    -- Select role information
    SELECT RoleId, RoleName, Description, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate
    FROM dbo.Roles
    WHERE RoleId = @RoleId;
END;
GO

SQL Code Smells & Rules for Tables, Functions, Triggers, Indexes and Sequences

1. Tables

Code Smells

Best Practices

2. Functions

Code Smells

Best Practices

3. Triggers

Code Smells

Best Practices

4. Indexes

Code Smells

Best Practices

5. Sequences

Code Smells

Best Practices

6. Stored Procedures

Common SQL Smells

Best Practices

This guide can serve as a reference for developers, DBAs, and reviewers to maintain clean, maintainable SQL code and avoid performance pitfalls.

Proper SQL commenting is not optional — it’s a professional standard. It improves readability, maintainability, and auditing. Following these standards ensures that your database is robust, team-friendly, and future-proof. Commenting may take a few extra minutes while writing code, but it saves hours during debugging and maintenance.

Thank you for taking the time to read this post. I hope it has provided you with a clear understanding of SQL Commenting Best Practices: Benefits, Standards & Examples. Proper SQL commenting and following coding standards may seem small, but they make a big difference in creating maintainable, readable, and professional database code. By implementing these best practices, you ensure your projects are team-friendly, robust, and easier to manage in the long run.

Happy coding and keep your SQL Code clean !!!