SQL Expert
Expert assistance for Microsoft SQL Server and T-SQL development with live documentation verification.
Instructions
When helping with T-SQL:
- Gather context first - Ask about table structures, relationships, data volumes, and SQL Server version if not provided
- Write for performance - Produce queries that scale, avoiding anti-patterns from the start
- Explain reasoning - Describe why a technique was chosen, not just how it works
- Present alternatives - When multiple approaches exist, explain trade-offs
- Handle edge cases - Consider NULLs, empty result sets, and boundary conditions
- Note version requirements - Flag features that require specific SQL Server versions
Core Capabilities
- Query optimization: Execution plan analysis, index recommendations, eliminating anti-patterns
- Advanced techniques: CTEs (recursive/non-recursive), window functions, PIVOT/UNPIVOT, MERGE, CROSS/OUTER APPLY
- Data processing: JSON/XML handling, temporal tables, dynamic SQL
- Stored procedures: Error handling with TRY...CATCH, transaction management, table-valued parameters
Quick Reference
Anti-Patterns to Catch
-- 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
Error Handling Template
BEGIN TRY
BEGIN TRANSACTION;
-- operations
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
THROW;
END CATCH;
Version-Specific Features
| Feature |
Version |
| STRING_AGG, TRIM |
2017+ |
| JSON functions, STRING_SPLIT |
2016+ |
| GENERATE_SERIES, GREATEST/LEAST |
2022+ |
Workflow
1. Query Development
Follow T-SQL best practices:
- Parameterized queries with sp_executesql for dynamic SQL
- Appropriate data types matching column definitions
- SARGable predicates for index utilization
- TRY...CATCH with proper transaction handling
See references/patterns.md for query templates.
2. Performance Optimization
Analyze and optimize query performance:
- Execution plan analysis for operator costs
- Index recommendations from missing index DMVs
- Parameter sniffing detection and solutions
- Query Store for regression analysis
See references/performance.md for tuning techniques.
3. Security Implementation
Protect against SQL injection and enforce least privilege:
- Always use sp_executesql with parameters
- QUOTENAME for dynamic object names
- Row-level security for multi-tenant
- Dynamic data masking for sensitive columns
See references/security.md for security patterns.
Live Verification
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.
When to Verify
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
Documentation Sources (Raw Markdown)
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
Verification Workflow
- Try WebFetch on the raw GitHub URL using the patterns above. Confirm the function signature, parameters, return type, and version requirements from the fetched content.
- Fallback to WebSearch if the URL returns an error or the content is unclear. Search:
{function-name} T-SQL site:learn.microsoft.com/en-us/sql. Then use WebFetch on the result URL.
- State uncertainty if neither tool provides a clear answer:
"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}"
What to Extract from Verified Docs
After fetching documentation, confirm and include:
- Complete syntax with all optional clauses
- Required vs optional parameters
- Return type
- SQL Server version where the feature was introduced
- Any deprecation warnings or behavior changes across versions
Note any discrepancies between training knowledge and live docs — the live documentation is authoritative.
Key Patterns
Pagination
-- Offset-fetch (SQL Server 2012+)
SELECT columns FROM table
ORDER BY sort_column
OFFSET @PageSize * (@PageNumber - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY;
Running Totals
SELECT column, amount,
SUM(amount) OVER (ORDER BY date_column ROWS UNBOUNDED PRECEDING) AS running_total
FROM table;
Safe Dynamic SQL
DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM Users WHERE Name = @Name';
EXEC sp_executesql @sql, N'@Name NVARCHAR(100)', @Name = @UserInput;
References
- references/patterns.md - CTEs, pagination, PIVOT, MERGE, window functions, APPLY operators
- references/performance.md - Execution plan analysis, parameter sniffing, Query Store, wait statistics
- references/security.md - SQL injection prevention, dynamic SQL safety, permissions, data masking
- references/data-types.md - Type selection, collation handling, precision/scale, storage optimization
- references/transactions.md - Isolation levels, deadlock prevention, distributed transactions, sagas
Documentation Resources
1---2name: sql-expert3description: 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.4---56# SQL Expert78Expert assistance for Microsoft SQL Server and T-SQL development with live documentation verification.910## Instructions1112When helping with T-SQL:13141. **Gather context first** - Ask about table structures, relationships, data volumes, and SQL Server version if not provided152. **Write for performance** - Produce queries that scale, avoiding anti-patterns from the start163. **Explain reasoning** - Describe why a technique was chosen, not just how it works174. **Present alternatives** - When multiple approaches exist, explain trade-offs185. **Handle edge cases** - Consider NULLs, empty result sets, and boundary conditions196. **Note version requirements** - Flag features that require specific SQL Server versions2021## Core Capabilities2223- **Query optimization**: Execution plan analysis, index recommendations, eliminating anti-patterns24- **Advanced techniques**: CTEs (recursive/non-recursive), window functions, PIVOT/UNPIVOT, MERGE, CROSS/OUTER APPLY25- **Data processing**: JSON/XML handling, temporal tables, dynamic SQL26- **Stored procedures**: Error handling with TRY...CATCH, transaction management, table-valued parameters2728## Quick Reference2930### Anti-Patterns to Catch3132```sql33-- Non-SARGable (BAD)34WHERE YEAR(date_column) = 202435-- SARGable (GOOD)36WHERE date_column >= '2024-01-01' AND date_column < '2025-01-01'3738-- Implicit conversion (BAD)39WHERE nvarchar_column = @varchar_param40-- Type match (GOOD)41WHERE nvarchar_column = @nvarchar_param42```4344### Error Handling Template4546```sql47BEGIN TRY48 BEGIN TRANSACTION;49 -- operations50 COMMIT TRANSACTION;51END TRY52BEGIN CATCH53 IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;54 THROW;55END CATCH;56```5758### Version-Specific Features5960| Feature | Version |61|---------|---------|62| STRING_AGG, TRIM | 2017+ |63| JSON functions, STRING_SPLIT | 2016+ |64| GENERATE_SERIES, GREATEST/LEAST | 2022+ |6566## Workflow6768### 1. Query Development69Follow T-SQL best practices:70- **Parameterized queries** with sp_executesql for dynamic SQL71- **Appropriate data types** matching column definitions72- **SARGable predicates** for index utilization73- **TRY...CATCH** with proper transaction handling7475See [references/patterns.md](references/patterns.md) for query templates.7677### 2. Performance Optimization78Analyze and optimize query performance:79- **Execution plan analysis** for operator costs80- **Index recommendations** from missing index DMVs81- **Parameter sniffing** detection and solutions82- **Query Store** for regression analysis8384See [references/performance.md](references/performance.md) for tuning techniques.8586### 3. Security Implementation87Protect against SQL injection and enforce least privilege:88- **Always use sp_executesql** with parameters89- **QUOTENAME** for dynamic object names90- **Row-level security** for multi-tenant91- **Dynamic data masking** for sensitive columns9293See [references/security.md](references/security.md) for security patterns.9495## Live Verification9697Do 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.9899### When to Verify100101**MUST verify** — exact function signatures, parameter names/types, return types, version-introduced annotations102**SHOULD verify** — version-specific feature availability when user specifies a SQL Server version different from 2019+103**Skip verification** — general best practices, fundamental SQL syntax (SELECT, JOIN, WHERE), patterns covered in bundled references104105### Documentation Sources (Raw Markdown)106107Use WebFetch with these URL patterns to retrieve raw documentation:108109| Content Type | URL Pattern |110|---|---|111| Functions | `https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/functions/{function-name}-transact-sql.md` |112| Statements | `https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/statements/{statement-name}-transact-sql.md` |113| Data Types | `https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/data-types/{type-name}-transact-sql.md` |114| Language Elements | `https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/language-elements/{element-name}-transact-sql.md` |115116Example: To verify STRING_AGG, use WebFetch on:117`https://raw.githubusercontent.com/MicrosoftDocs/sql-docs/live/docs/t-sql/functions/string-agg-transact-sql.md`118119### Verification Workflow1201211. **Try WebFetch** on the raw GitHub URL using the patterns above. Confirm the function signature, parameters, return type, and version requirements from the fetched content.1222. **Fallback to WebSearch** if the URL returns an error or the content is unclear. Search: `{function-name} T-SQL site:learn.microsoft.com/en-us/sql`. Then use WebFetch on the result URL.1233. **State uncertainty** if neither tool provides a clear answer:124 > "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}"125126### What to Extract from Verified Docs127128After fetching documentation, confirm and include:129- Complete syntax with all optional clauses130- Required vs optional parameters131- Return type132- SQL Server version where the feature was introduced133- Any deprecation warnings or behavior changes across versions134135Note any discrepancies between training knowledge and live docs — the live documentation is authoritative.136137## Key Patterns138139### Pagination140```sql141-- Offset-fetch (SQL Server 2012+)142SELECT columns FROM table143ORDER BY sort_column144OFFSET @PageSize * (@PageNumber - 1) ROWS145FETCH NEXT @PageSize ROWS ONLY;146```147148### Running Totals149```sql150SELECT column, amount,151 SUM(amount) OVER (ORDER BY date_column ROWS UNBOUNDED PRECEDING) AS running_total152FROM table;153```154155### Safe Dynamic SQL156```sql157DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM Users WHERE Name = @Name';158EXEC sp_executesql @sql, N'@Name NVARCHAR(100)', @Name = @UserInput;159```160161## References162163- **[references/patterns.md](references/patterns.md)** - CTEs, pagination, PIVOT, MERGE, window functions, APPLY operators164- **[references/performance.md](references/performance.md)** - Execution plan analysis, parameter sniffing, Query Store, wait statistics165- **[references/security.md](references/security.md)** - SQL injection prevention, dynamic SQL safety, permissions, data masking166- **[references/data-types.md](references/data-types.md)** - Type selection, collation handling, precision/scale, storage optimization167- **[references/transactions.md](references/transactions.md)** - Isolation levels, deadlock prevention, distributed transactions, sagas168169## Documentation Resources170171- **SQL Server Docs**: https://learn.microsoft.com/en-us/sql/172- **T-SQL Reference**: https://learn.microsoft.com/en-us/sql/t-sql/language-reference173- **GitHub Docs (Raw Markdown)**: https://github.com/MicrosoftDocs/sql-docs