Skip to content

ludwigdmark/T-SQL-Scripts

Repository files navigation

T-SQL Scripts

Overview

This repository is a comprehensive, production-grade collection of reusable T-SQL scripts, including scalar-valued functions, table-valued functions, stored procedures, and views. The scripts are designed to address a wide range of database administration, automation, data transformation, and utility needs for Microsoft SQL Server environments. The codebase is intended for use by database administrators (DBAs), developers, and DevOps engineers who require robust, well-documented, and reusable SQL Server tools.

Key Features

  • Extensive Coverage: Includes scripts for array manipulation, string processing, file and OS operations, database maintenance, monitoring, and more.
  • Modular Design: Each script is self-contained and documented, making it easy to integrate into existing environments or automation pipelines.
  • Production Ready: Scripts are written with error handling, input validation, and performance in mind.
  • Cross-Component Integration: Many scripts leverage SQL Server features such as xp_cmdshell, OLE Automation, and PowerShell integration for advanced automation.
  • Documentation: All scripts follow a standardized header template with summary, parameters, output, usage examples, dependencies, and validation notes.

⚠️ Warnings & Critical Usage Notes

  • Permissions: Many scripts require elevated permissions (e.g., sysadmin, CONTROL SERVER, or VIEW SERVER STATE). Always review and test scripts in a non-production environment before deploying to production.
  • File System & OS Access: Scripts that interact with the file system or OS (e.g., backup, file write, PowerShell execution) require xp_cmdshell or OLE Automation to be enabled and may expose the server to security risks. Only enable these features if absolutely necessary and always follow your organization's security policies.
  • Data Loss Risk: Some procedures (e.g., object drops, user kills, file operations) can result in data loss or service disruption if misused. Double-check parameters and always maintain backups.
  • Version Compatibility: Scripts are designed for SQL Server 2012 and later. Some features (e.g., STRING_SPLIT, STRING_AGG, OPENJSON) require SQL Server 2016+.
  • Environment-Specific Paths: File and backup paths are environment-specific. Update paths and test access before running scripts.
  • Performance: Some scripts (especially those that scan system catalogs or perform large data operations) may impact server performance. Schedule heavy operations during maintenance windows.

Best Practices

  1. Schema Prefix: Always use the dbo. schema prefix when referencing objects to avoid ambiguity and improve performance.
  2. Permissions: Review and grant only the minimum required permissions for each script. Avoid running as sa unless necessary.
  3. Testing: Test all scripts in a development or staging environment before production use. Validate outputs and error handling.
  4. Change Management: Use source control and change tracking for all script modifications. Document changes in the script header.
  5. Security: Minimize use of xp_cmdshell and OLE Automation. Disable these features when not in use. Never expose sensitive credentials in scripts.
  6. Error Handling: Review error handling in each script. Add TRY/CATCH blocks or validation logic as needed for your environment.
  7. Documentation: Maintain and update script headers with accurate summaries, parameter descriptions, and usage examples.
  8. Backup: Always maintain recent backups before running scripts that modify schema, data, or server configuration.
  9. Monitoring: Use the provided views and monitoring scripts to proactively identify issues and optimize performance.
  10. Review Dependencies: Check for dependencies on external binaries (e.g., 7zip for backup compression) and ensure they are installed and accessible.

Contribution Guidelines

  • Follow the established header template for all new scripts.
  • Use clear, descriptive names following the naming conventions (udf_, usp_, vw_, etc.).
  • Include example usage and expected output in the header.
  • Document any environment-specific requirements or dependencies.
  • Submit pull requests with a clear description of changes and testing performed.

Project Structure

Project Structure

Scalar-valued Functions

Array Operations

Database Administration

File System Operations

String and Formatting

Stored Procedures

Database Administration

HTTP and File Operations

Table-valued Functions

Views

Usage

Most functions and procedures include detailed headers with usage examples. Always check permissions before executing DBA operations, especially those requiring sysadmin or elevated permissions.

Requirements

  • SQL Server 2012 or later
  • Some functions require xp_cmdshell for file system operations
  • PowerShell access for certain administrative tasks
  • Appropriate permissions for DBA operations

Best Practices

  1. Always use schema prefix (dbo.) when referencing objects
  2. Check required permissions before executing DBA operations
  3. Test file system operations in a safe environment first
  4. Review procedure headers for specific requirements and examples
  5. Follow SQL Server security best practices when enabling features like xp_cmdshell

About

A useful collection of T-SQL procs, functions and views.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages