This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In the previous articles, I have shared basic about the SQL Server PowerShell (sqlps) and how we can navigate SQL Server instance data using PowerShell.
In the SQLPS module, there are different commands available to extensively work with SQL Server.
The cmdlet command type contains most of the commands of SQLPS.
The Invoke-SQLCMD is also one command to execute T-SQL in PowerShell.
Here, I am sharing some of the examples of Invoke-SQLCMD to execute T-SQL in PowerShell.
First, list of all commands of SQLPS:
1 |
Get-command –module SQLPS |
Check SQL Server Version:
1 |
Invoke-SQLCMD "select @@version;" |
Check Default database name:
1 |
Invoke-SQLCMD "select db_name();" |
Execute SELECT statement:
1 |
Invoke-SQLCMD -Query "SELECT *FROM dbo.tbl_TestSnapshot;" –Database "dbrnd" |
Execute SELECT with PowerShell Variable:
1 2 |
$MyVar = "Anvesh" Invoke-SQLCMD -Query "SELECT *FROM dbo.tbl_TestSnapshot WHERE Name = '$($MyVar)';" –Database "dbrnd" |