Files
sql-utilities/docs/SqlUtilities.SqlServer.md
T

1.8 KiB

Strata.SqlTools.SqlServer

Microsoft SQL Server T-SQL specific implementations for the Strata.SqlTools library.

Features

  • QueryBreakdown: Parse and generate T-SQL SELECT queries
  • DeleteBreakdown: Parse and generate DELETE statements
  • InsertBreakdown: Parse and generate INSERT statements
  • UpdateBreakdown: Parse and generate UPDATE statements
  • ProcedureBreakdown: Parse and generate stored procedure EXEC calls
  • Statement Parsing: Token-based T-SQL parsing
  • Command Visitor: T-SQL specific SQL generation

Installation

dotnet add package Strata.SqlTools.SqlServer

Usage

using Strata.SqlTools.Breakdowns.SqlServer;

// Parse a T-SQL query
var query = QueryBreakdown.Parse(@"
    SELECT CustomerID, CustomerName
    FROM Customers
    WHERE Region = @region
");

// Parameters are automatically extracted during parsing
Assert.That(query.Parameters, Does.ContainKey("@region"));

// Modify and regenerate - parameters are automatically extracted
query.AddWhereClause("IsActive = 1", "and");
query.AddWhereClause("CreatedDate > @startDate", "and");

// The @startDate parameter is now in the Parameters dictionary
query.SetParameterValue("@startDate", "2024-01-01");
query.SetParameterValue("@region", "West");

string sql = query.GetSql();

Automatic Parameter Extraction

The AddWhereClause method automatically extracts @parameter references and adds them to the Parameters dictionary:

  • Parameters are created with null values initially
  • Use SetParameterValue to assign actual values
  • Existing parameter values are preserved when adding additional WHERE clauses
  • Type mismatches throw InvalidOperationException for safety

Dependencies

  • Strata.SqlTools (core library)
  • System.Data.SqlClient

License

MIT License - see LICENSE.txt for details