< Back

Invoke-DbaQuery

Mon Jan 13, 2020 12:27 pm

NAME Invoke-DbaQuery



SYNOPSIS

A command to run explicit T-SQL commands or files.





SYNTAX

Invoke-DbaQuery [-SqlInstance <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [-SqlCredential

<Pscredential>] [-Database <String>] -Query <String> [-QueryTimeout <Int>] [-As <String>] [-SqlParameters

<System.Collections.IDictionary>] [-AppendServerInstance <Switch>] [-MessagesToOutput <Switch>] [-InputObject

<Microsoft.SqlServer.Management.Smo.Database[]>] [-ReadOnly <Switch>] [-EnableException <Switch>]

[<CommonParameters>]



Invoke-DbaQuery [-SqlInstance <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [-SqlCredential

<Pscredential>] [-Database <String>] [-QueryTimeout <Int>] -File <System.Object[]> [-As <String>] [-SqlParameters

<System.Collections.IDictionary>] [-AppendServerInstance <Switch>] [-MessagesToOutput <Switch>] [-InputObject

<Microsoft.SqlServer.Management.Smo.Database[]>] [-ReadOnly <Switch>] [-EnableException <Switch>]

[<CommonParameters>]



Invoke-DbaQuery [-SqlInstance <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [-SqlCredential

<Pscredential>] [-Database <String>] [-QueryTimeout <Int>] -SqlObject

<Microsoft.SqlServer.Management.Smo.SqlSmoObject[]> [-As <String>] [-SqlParameters

<System.Collections.IDictionary>] [-AppendServerInstance <Switch>] [-MessagesToOutput <Switch>] [-InputObject

<Microsoft.SqlServer.Management.Smo.Database[]>] [-ReadOnly <Switch>] [-EnableException <Switch>]

[<CommonParameters>]





DESCRIPTION

This function is a wrapper command around Invoke-DbaAsync, which in turn is based on Invoke-SqlCmd2.



It was designed to be more convenient to use in a pipeline and to behave in a way consistent with the rest of our

functions.





PARAMETERS

-AppendServerInstance [<Switch>]

If this switch is enabled, the SQL Server instance will be appended to PSObject and DataRow output.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-As [<String>]

Specifies output type. Valid options for this parameter are 'DataSet', 'DataTable', 'DataRow', 'PSObject', and

'SingleValue'



PSObject output introduces overhead but adds flexibility for working with results:

http://powershell.org/wp/forums/topic/d ... th-dbnull/



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Database [<String>]

The database to select before running the query. This list is auto-populated from the server.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.

This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables

advanced scripting.

Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own

try/catch.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-File [<System.Object[]>]

Specifies the path to one or several files to be used as the query input.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-InputObject [<Microsoft.SqlServer.Management.Smo.Database[]>]

A collection of databases (such as returned by Get-DbaDatabase)



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-MessagesToOutput [<Switch>]

Use this switch to have on the output stream messages too (e.g. PRINT statements). Output will hold the

resultset too. See examples for detail



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Query [<String>]

Specifies one or more queries to be run. The queries can be Transact-SQL, XQuery statements, or sqlcmd

commands. Multiple queries in a single batch may be separated by a semicolon or a GO



Escape any double quotation marks included in the string.



Consider using bracketed identifiers such as [MyTable] instead of quoted identifiers such as "MyTable".



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-QueryTimeout [<Int>]

Specifies the number of seconds before the queries time out.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ReadOnly [<Switch>]

Execute the query with ReadOnly application intent.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlCredential [<Pscredential>]

Credential object used to connect to the SQL Server Instance as a different user. This can be a Windows or SQL

Server account. Windows users are determined by the existence of a backslash, so if you are intending to use

an alternative Windows connection instead of a SQL login, ensure it contains a backslash.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the

function to be executed against multiple SQL Server instances.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlObject [<Microsoft.SqlServer.Management.Smo.SqlSmoObject[]>]

Specify one or more SQL objects. Those will be converted to script and their scripts run on the target

system(s).



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlParameters [<System.Collections.IDictionary>]

Specifies a hashtable of parameters for parameterized SQL queries.

http://blog.codinghorror.com/give-me-pa ... -me-death/



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Database, Query

Author: Friedrich Weinmann (@FredWeinmann)



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Invoke-DbaQuery -SqlInstance server\\instance -Query 'SELECT foo FROM bar'



Runs the sql query 'SELECT foo FROM bar' against the instance 'server\\instance'

-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Get-DbaRegServer -SqlInstance [SERVERNAME] -Group [GROUPNAME] | Invoke-DbaQuery -Query 'SELECT foo FROM bar'



Runs the sql query 'SELECT foo FROM bar' against all instances in the group [GROUPNAME] on the CMS [SERVERNAME]

-------------------------- EXAMPLE 3 --------------------------



PS C:\\>"server1", "server1\\nordwind", "server2" | Invoke-DbaQuery -File "C:\\scripts\\sql\\rebuild.sql"



Runs the sql commands stored in rebuild.sql against the instances "server1", "server1\\nordwind" and "server2"

-------------------------- EXAMPLE 4 --------------------------



PS C:\\>Get-DbaDatabase -SqlInstance "server1", "server1\\nordwind", "server2" | Invoke-DbaQuery -File

"C:\\scripts\\sql\\rebuild.sql"



Runs the sql commands stored in rebuild.sql against all accessible databases of the instances "server1",

"server1\\nordwind" and "server2"

-------------------------- EXAMPLE 5 --------------------------



PS C:\\>Invoke-DbaQuery -SqlInstance . -Query 'SELECT * FROM users WHERE Givenname = @name' -SqlParameters @{ Name

= "Maria" }



Executes a simple query against the users table using SQL Parameters.

This avoids accidental SQL Injection and is the safest way to execute queries with dynamic content.

Keep in mind the limitations inherent in parameters - it is quite impossible to use them for content references.

While it is possible to parameterize a where condition, it is impossible to use this to select which columns to

select.

The inserted text will always be treated as string content, and not as a reference to any SQL entity (such as

columns, tables or databases).

-------------------------- EXAMPLE 6 --------------------------



PS C:\\>Invoke-DbaQuery -SqlInstance aglistener1 -ReadOnly -Query "select something from readonlydb.dbo.atable"



Executes a query with ReadOnly application intent on aglistener1.



RELATED LINKS

https://dbatools.io/Invoke-DbaQuery