Skip to content

Instantly share code, notes, and snippets.

@doggy8088
Created October 21, 2025 06:49
Show Gist options
  • Select an option

  • Save doggy8088/6cedc1d60a3d2a0063415666091a4245 to your computer and use it in GitHub Desktop.

Select an option

Save doggy8088/6cedc1d60a3d2a0063415666091a4245 to your computer and use it in GitHub Desktop.
description tools
Guidelines for generating SQL statements and stored procedures
changes
codebase
fetch
problems
runCommands
runTasks
search
searchResults
terminalLastCommand
terminalSelection

SQL Development

Database schema design

  • all tables should have a primary key constraint
  • all foreign key constraints should have a name
  • all foreign key constraints should be defined inline
  • all foreign key constraints should have ON DELETE CASCADE option
  • all foreign key constraints should have ON UPDATE CASCADE option
  • all foreign key constraints should reference the primary key of the parent table

SQL Coding Style

  • use uppercase for SQL keywords (SELECT, FROM, WHERE)
  • use consistent indentation for nested queries and conditions
  • include comments to explain complex logic
  • break long queries into multiple lines for readability
  • organize clauses consistently (SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY)
  • when using string literals, always use N'xxx' syntax for Unicode strings (NVARCHAR)

SQL Query Structure

  • use explicit column names in SELECT statements instead of SELECT *
  • qualify column names with table name or alias when using multiple tables
  • limit the use of subqueries when joins can be used instead
  • include LIMIT/TOP clauses to restrict result sets
  • use appropriate indexing for frequently queried columns
  • avoid using functions on indexed columns in WHERE clauses

Stored Procedure Naming Conventions

  • prefix stored procedure names with 'sp_'
  • use PascalCase for stored procedure names
  • use descriptive names that indicate purpose (e.g., sp_GetCustomerOrders)
  • include plural noun when returning multiple records (e.g., sp_GetProducts)
  • include singular noun when returning single record (e.g., sp_GetProduct)

Parameter Handling

  • prefix parameters with '@'
  • use camelCase for parameter names
  • provide default values for optional parameters
  • validate parameter values before use
  • document parameters with comments
  • arrange parameters consistently (required first, optional later)

Stored Procedure Structure

  • include header comment block with description, parameters, and return values
  • return standardized error codes/messages
  • return result sets with consistent column order
  • use OUTPUT parameters for returning status information
  • prefix temporary tables with 'tmp_'

SQL Security Best Practices

  • parameterize all queries to prevent SQL injection
  • use prepared statements when executing dynamic SQL
  • avoid embedding credentials in SQL scripts
  • implement proper error handling without exposing system details
  • avoid using dynamic SQL within stored procedures

Transaction Management

  • explicitly begin and commit transactions
  • use appropriate isolation levels based on requirements
  • avoid long-running transactions that lock tables
  • use batch processing for large data operations
  • include SET NOCOUNT ON for stored procedures that modify data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment