Write, optimize, and debug T-SQL queries for Microsoft SQL Server. Covers CTEs, window functions, PIVOT, MERGE, APPLY operators, execution plan analysis, indexing strategies, and stored procedures. Use when working with SQL Server, T-SQL scripts, .sql files, stored procedures, query optimization, or database performance tuning.
Expert assistance for Microsoft SQL Server and T-SQL development with live documentation verification.
When helping with T-SQL:
-- Non-SARGable (BAD)
WHERE YEAR(date_column) = 2024
-- SARGable (GOOD)
WHERE date_column >= '2024-01-01' AND date_column < '2025-01-01'
-- Implicit conversion (BAD)
WHERE nvarchar_column = @varchar_param
-- Type match (GOOD)
WHERE nvarchar_column = @nvarchar_param
BEGIN TRY
BEGIN TRANSACTION;
-- operations
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
THROW;
END CATCH;
| Feature | Version |
|---|---|
| STRING_AGG, TRIM | 2017+ |
| JSON functions, STRING_SPLIT | 2016+ |
| GENERATE_SERIES, GREATEST/LEAST | 2022+ |
Follow T-SQL best practices:
See references/patterns.md for query templates.
Analyze and optimize query performance:
See references/performance.md for tuning techniques.
Protect against SQL injection and enforce least privilege:
See references/security.md for security patterns.
Do not rely solely on training data for exact T-SQL syntax, parameter lists, or version-specific behavior. Use WebFetch and WebSearch to verify against official Microsoft documentation.
MUST verify — exact function signatures, parameter names/types, return types, version-introduced annotations SHOULD verify — version-specific feature availability when user specifies a SQL Server version different from 2019+ Skip verification — general best practices, fundamental SQL syntax (SELECT, JOIN, WHERE), patterns covered in bundled references
Use WebFetch with these URL patterns to retrieve raw documentation:
| Content Type | URL Pattern |
|---|---|
| Functions | https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/functions/{function-name}-transact-sql.md |
| Statements | https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/statements/{statement-name}-transact-sql.md |
| Data Types | https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/data-types/{type-name}-transact-sql.md |
| Language Elements | https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/language-elements/{element-name}-transact-sql.md |
Example: To verify STRING_AGG, use WebFetch on:
https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/functions/string-agg-transact-sql.md
{function-name} T-SQL site:learn.microsoft.com/en-us/sql. Then use WebFetch on the result URL."I wasn't able to verify this syntax against live documentation. Please confirm at: https://learn.microsoft.com/en-us/sql/t-sql/functions/{function-name}"
After fetching documentation, confirm and include:
Note any discrepancies between training knowledge and live docs — the live documentation is authoritative.
-- Offset-fetch (SQL Server 2012+)
SELECT columns FROM table
ORDER BY sort_column
OFFSET @PageSize * (@PageNumber - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY;
SELECT column, amount,
SUM(amount) OVER (ORDER BY date_column ROWS UNBOUNDED PRECEDING) AS running_total
FROM table;
DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM Users WHERE Name = @Name';
EXEC sp_executesql @sql, N'@Name NVARCHAR(100)', @Name = @UserInput;