|
| 1 | +--- |
| 2 | +applyTo: '**/*.sql' |
| 3 | + |
| 4 | +description: 'This file contains instructions for T-SQL development. |
| 5 | + It includes guidelines for database schema design, query optimization, and following SQL coding standards. |
| 6 | + Ensure to follow the practices outlined in this file to maintain code quality and consistency.' |
| 7 | +--- |
| 8 | + |
| 9 | +# T-SQL Development Instructions |
| 10 | + |
| 11 | +## General |
| 12 | + |
| 13 | +* MUST follow standard T-SQL coding conventions for naming, indentation, and spacing. |
| 14 | +* MUST implement robust error handling with TRY-CATCH blocks and meaningful error messages. |
| 15 | +* MUST always review AI-generated SQL code for correctness, security, and performance. |
| 16 | +* MUST parameterize all queries to prevent SQL injection attacks. |
| 17 | +* MUST consider performance, maintainability, and business impact when designing database solutions. |
| 18 | + |
| 19 | +## Database Schema Design |
| 20 | + |
| 21 | +Follow these database design principles for consistency across the project. |
| 22 | + |
| 23 | +* MUST use singular form for all table names (e.g., `Customer`, not `Customers`). |
| 24 | +* MUST use singular form for all column names (e.g., `Name`, not `Names`). |
| 25 | +* MUST include a primary key column named `Id` in all tables. |
| 26 | +* MUST include a `CreatedAt` column to store the creation timestamp in all tables. |
| 27 | +* MUST include an `UpdatedAt` column to store the last update timestamp in all tables. |
| 28 | + |
| 29 | +## Constraints and Relationships |
| 30 | + |
| 31 | +* MUST define primary key constraints on all tables. |
| 32 | +* MUST provide explicit names for all foreign key constraints. |
| 33 | +* MUST define foreign key constraints inline with column definitions. |
| 34 | +* MUST use `ON DELETE CASCADE` option for foreign key constraints where appropriate. |
| 35 | +* MUST use `ON UPDATE CASCADE` option for foreign key constraints where appropriate. |
| 36 | +* MUST ensure foreign key constraints reference the primary key of the parent table. |
| 37 | + |
| 38 | +## Code Style |
| 39 | + |
| 40 | +Follow these T-SQL coding conventions for consistency across the project. |
| 41 | + |
| 42 | +* MUST use UPPERCASE for SQL keywords (`SELECT`, `FROM`, `WHERE`, `INSERT`, `UPDATE`, `DELETE`). |
| 43 | +* MUST use consistent indentation (4 spaces) for nested queries and conditional statements. |
| 44 | +* MUST qualify column names with table name or alias when using multiple tables. |
| 45 | +* MUST organize query clauses consistently: `SELECT`, `FROM`, `JOIN`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`. |
| 46 | +* MUST break long queries into multiple lines for improved readability. |
| 47 | +* MUST quote identifiers (schema names, table names, column names and alias names) with square brackets `[]` when necessary to avoid conflicts with reserved keywords. |
| 48 | +* MUST use meaningful table aliases (e.g., `cust` for Customer, `ord` for Order) instead of single letters. |
| 49 | +* MUST align commas consistently (either leading or trailing, but be consistent within the project). |
| 50 | +* MUST use proper spacing around operators (`=`, `<>`, `>`, `<`, etc.). |
| 51 | + |
| 52 | +## Query Structure and Performance |
| 53 | + |
| 54 | +* MUST use explicit column names in `SELECT` statements instead of `SELECT *`. |
| 55 | +* MUST limit the use of subqueries when `JOIN` operations can be used instead. |
| 56 | +* MUST include `TOP` or `OFFSET`/`FETCH` clauses to restrict result sets when appropriate. |
| 57 | +* MUST avoid using functions on indexed columns in `WHERE` clauses. |
| 58 | +* MUST use appropriate indexing strategies for frequently queried columns. |
| 59 | +* MUST use `EXISTS` instead of `IN` when checking for existence with subqueries. |
| 60 | +* MUST use `UNION ALL` instead of `UNION` when duplicates are acceptable for better performance. |
| 61 | +* MUST consider query execution plans and optimize based on actual performance metrics. |
| 62 | +* MUST use Common Table Expressions (CTEs) for complex queries to improve readability. |
| 63 | +* MUST implement pagination using `OFFSET` and `FETCH NEXT` for modern T-SQL versions. |
| 64 | + |
| 65 | +## Stored Procedures |
| 66 | + |
| 67 | +### Naming Conventions |
| 68 | + |
| 69 | +* MUST prefix stored procedure names with `usp_` (e.g., `usp_GetCustomerOrders`). |
| 70 | +* MUST use PascalCase for stored procedure names. |
| 71 | +* MUST use descriptive names that clearly indicate the procedure's purpose. |
| 72 | +* MUST use plural nouns when returning multiple records (e.g., `usp_GetProducts`). |
| 73 | +* MUST use singular nouns when returning a single record (e.g., `usp_GetProduct`). |
| 74 | +* MUST include the entity name in the procedure name for clarity (e.g., `usp_CreateCustomer`, `usp_UpdateOrder`). |
| 75 | + |
| 76 | +### Parameter Handling |
| 77 | + |
| 78 | +* MUST prefix all parameters with `@` symbol. |
| 79 | +* MUST use camelCase for parameter names (e.g., `@customerId`, `@orderDate`). |
| 80 | +* MUST provide default values for optional parameters. |
| 81 | +* MUST validate parameter values before processing. |
| 82 | +* MUST document all parameters with descriptive comments. |
| 83 | +* MUST arrange parameters consistently (required parameters first, optional parameters later). |
| 84 | + |
| 85 | +### Structure and Documentation |
| 86 | + |
| 87 | +* MUST include a header comment block with procedure description, parameter details, and return values. |
| 88 | +* MUST return standardized error codes and meaningful error messages. |
| 89 | +* MUST return result sets with consistent column ordering. |
| 90 | +* MUST use `OUTPUT` parameters for returning status information when appropriate. |
| 91 | +* MUST prefix temporary tables with `tmp_` (e.g., `tmp_CustomerData`). |
| 92 | + |
| 93 | +## Security Best Practices |
| 94 | + |
| 95 | +* MUST parameterize all dynamic queries to prevent SQL injection vulnerabilities. |
| 96 | +* MUST use prepared statements when executing dynamic SQL. |
| 97 | +* MUST avoid embedding credentials or sensitive information in SQL scripts. |
| 98 | +* MUST implement proper error handling without exposing internal system details. |
| 99 | +* MUST minimize the use of dynamic SQL within stored procedures. |
| 100 | + |
| 101 | +## Transaction Management |
| 102 | + |
| 103 | +* MUST explicitly begin and commit transactions using `BEGIN TRANSACTION` and `COMMIT`. |
| 104 | +* MUST use appropriate isolation levels based on business requirements. |
| 105 | +* MUST avoid long-running transactions that could cause table locking issues. |
| 106 | +* MUST use batch processing techniques for large data operations. |
| 107 | +* MUST include `SET NOCOUNT ON` in stored procedures that modify data. |
| 108 | +* MUST implement proper rollback logic in error handling scenarios. |
| 109 | + |
| 110 | +## Comments and Documentation |
| 111 | + |
| 112 | +* MUST include comments to explain complex business logic and query operations. |
| 113 | +* MUST document any non-obvious performance optimizations or workarounds. |
| 114 | +* MUST provide examples of expected input parameters and result sets in procedure headers. |
0 commit comments