Created
July 28, 2015 18:35
-
-
Save Kevin-Bronsdijk/55d369da587715c55daf to your computer and use it in GitHub Desktop.
executing-sql-statements-using-powershell2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module SQLPS -DisableNameChecking | |
#replace your with your instance name | |
$instanceName = "server\instance"; | |
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName; | |
#replace your with your user database name | |
$dbName = "name_user_db" | |
$db = $srv.Databases[$dbName] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Similar to a ado.net execute non query | |
Invoke-Sqlcmd ` | |
-ServerInstance "$instanceName" ` | |
-Database $dbName ` | |
-Query "INSERT INTO [dbo].[test1] ([column1]) VALUES (1)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#execute query and export to a CSV | |
Invoke-Sqlcmd ` | |
-ServerInstance "$instanceName" ` | |
-Database $dbName ` | |
-Query "SELECT TOP 1000 [column1],[id] FROM [dbo].[test1]" | | |
Export-Csv -LiteralPath "C:\results.csv" ` | |
-NoTypeInformation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment