liveBook · Manning

SQL parameter

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

An SQL parameter is a separately supplied value associated with a named placeholder in a SQL statement.

An SQL parameter supplies a value separately from a named placeholder in a SQL statement. In PowerShell, write the placeholder with @, then pass its value through the SqlParameter hashtable to Invoke-DbaQuery. section 7.4.1

Parameterization improves dynamic queries by:

  • Checking data types and converting values such as DateTime from PowerShell to SQL format. section 7.4.1
  • Escaping characters safely, enabling shared query plans, and reducing SQL injection risk. section 7.4.1
  • Treating malicious-looking input as a value rather than executable SQL code. section 7.4.1

Parameterized server lookup

Use Query = 'SELECT * FROM Servers WHERE Name = @name' with SqlParameter = @{name = 'Srv01'} instead of interpolating the server name into the query string. section 7.4.1

$DbaQuery = @{
    SqlInstance = "$($env:COMPUTERNAME)\SQLEXPRESS"
    Database = 'PoshAssetMgmt'
    Query = 'SELECT * FROM Servers WHERE Name = @name'
    SqlParameter = @{name = 'Srv01'}
}
Invoke-DbaQuery @DbaQuery

Shows the complete Invoke-DbaQuery argument hashtable with a named SQL placeholder and its separately supplied value. section 7.4.1

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains placeholders, hashtable binding, type conversion, escaping, query plans, and injection prevention.
Examples
Uses a server-name lookup and contrasts parameter binding with interpolated SQL that can execute injected commands.
What it assumes
Assumes basic SQL filtering and PowerShell hashtable usage while demonstrating parameter binding from the relevant mechanics.
What you can do after
Apply parameters via Invoke-DbaQuery to build safer dynamic SQL filters.
How it connects
Connects SQL parameters with WHERE clauses, PowerShell hashtables, Invoke-DbaQuery, query plans, and SQL injection.

Also explained in

Compare how 2 books explain SQL parameter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.