Sqlserver

From G1Joshi/Agent-Skills · View interactive version

Sqlserver is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Microsoft SQL Server assistance covering T-SQL, execution plans, index tuning, Always On availability groups, and DMV profiling. Use when developing enterprise .NET applications, administering SQL Server, or optimizing T-SQL.

How to use this skill

Skill file

SQL Server (MSSQL)

Microsoft SQL Server is an enterprise-grade RDBMS. It uses T-SQL (Transact-SQL), an extension of SQL that adds procedural programming, local variables, and data processing.

When to Use

Quick Start

-- CTE and Window Function
WITH Sales_CTE AS (
    SELECT SalesPersonID, SUM(TotalDue) AS TotalSales
    FROM Sales.SalesOrderHeader
    GROUP BY SalesPersonID
)
SELECT SalesPersonID, TotalSales,
       RANK() OVER (ORDER BY TotalSales DESC) AS SalesRank
FROM Sales_CTE;

Core Concepts

System-Versioned Temporal Tables

Automatically captures complete historical audit trails for every row mutation:

CREATE TABLE dbo.Employee (
    EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
    FullName NVARCHAR(100) NOT NULL,
    Department NVARCHAR(50) NOT NULL,
    Salary DECIMAL(10,2) NOT NULL,
    SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN,
    SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN,
    PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime)
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmployeeHistory));

-- Query row state as of specific point in time
SELECT * FROM dbo.Employee
FOR SYSTEM_TIME AS OF '2026-01-15 12:00:00'
WHERE EmployeeID = 101;

Always On Availability Groups

Synchronous and asynchronous multi-database replication across high-availability failover nodes:

[ Primary Replica (Read/Write) ] ──Synchronous Commit──→ [ Secondary Replica (Readable) ]
                                          │
                                          ▼
                               [ Disaster Recovery Replica (Async) ]

Query Store & Plan Forcing

Captures query performance history, runtime statistics, and forces stable execution plans:

-- Enable Query Store
ALTER DATABASE EnterpriseDB SET QUERY_STORE = ON;

-- Force specific compiled execution plan to stop regression
EXEC sp_query_store_force_plan @query_id = 42, @plan_id = 108;

Common Patterns

Index with Included Columns for Covering Queries

Problem: Wide indexes waste storage and index page space, while narrow indexes trigger expensive Key Lookups.

Solution:

Create index with search columns in key and projection columns in INCLUDE:

CREATE NONCLUSTERED INDEX IX_Orders_CustomerId_Status
ON Sales.Orders (CustomerId, OrderStatus)
INCLUDE (OrderDate, TotalAmount);

-- Covering query executes purely from index pages without Key Lookup
SELECT OrderDate, TotalAmount
FROM Sales.Orders
WHERE CustomerId = 1205 AND OrderStatus = 'Shipped';

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :-------------------------------------------------------------------- | :-------------------------------------------------------------- | :-------------------------------------------------------------------------------- |

| Transaction (Process ID ...) was deadlocked on lock resources | Two transactions competing for locks in reverse sequence. | Implement try-catch retry logic; ensure consistent object access order. |

| Could not allocate space for object in database ... tablespace full | Datafile or transaction log disk storage exhausted. | Check autogrowth settings, backup transaction log (BACKUP LOG), or expand disk. |

| Implicit conversion causing index scan | Data type mismatch (e.g. VARCHAR comparing to NVARCHAR column). | Align parameter types with column definitions in application queries. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.