SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert data into a SQL Server Table using Powershell using Invoke-SQLc


Insert data into a SQL Server Table using Powershell using Invoke-SQLc

Author
Message
VishnuGupthanSQLPowershellDBA
VishnuGupthanSQLPowershellDBA
Right there with Babe
Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)

Group: General Forum Members
Points: 777 Visits: 653
Comments posted to this topic are about the item Insert data into a SQL Server Table using Powershell using Invoke-SQLc
Ali M
Ali M
SSC Veteran
SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)

Group: General Forum Members
Points: 219 Visits: 80
Much simpler and more efficient version (only one round-trip to the SQL server).
If you wanted to update the table with the current state, the valueList could be used to form a MERGE statement.


# Get all services to a list of VALUEs for insert
# -join ensures each row is separated with "comma, carriage return" except for the last one
$valueList = (Get-Service | ForEach-Object { "( '$($_.Status)', '$($_.Name)', '$($_.DisplayName)')" }) -join ",`n"
$insertquery=@"
INSERT INTO [dbo].[ServiceTable]
([Status]
,[Name]
,[DisplayName])
VALUES
$valueList
"@
Invoke-SQLcmd -ServerInstance 'KILIKOOD-PC\MSSQLSERVER,1433' -query $insertQuery -U sa -P test123 -Database Fantasy


MadAdmin
MadAdmin
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10060 Visits: 2060
using DBATools

$datatable = Import-Csv C:\temp\customers.csv | Out-DbaDataTable
Write-DbaDataTable -SqlServer sql2014 -InputObject $datatable -Table mydb.dbo.customers

Much better than foreach

Catch-all queries done right
Gail Shaw's Performance Blog
Sue_H
Sue_H
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72640 Visits: 14937
MadAdmin - Thursday, February 22, 2018 6:54 AM
using DBATools

$datatable = Import-Csv C:\temp\customers.csv | Out-DbaDataTable
Write-DbaDataTable -SqlServer sql2014 -InputObject $datatable -Table mydb.dbo.customers

Much better than foreach


Importing data from an existing file is not at all what they are doing though.
They are getting all services and then for each service, get the service name, status and display name.
But as the author explained, it's an example for using Invoke-SQLcmd.

Sue



MadAdmin
MadAdmin
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10060 Visits: 2060
Sue_H - Thursday, February 22, 2018 7:32 AM
MadAdmin - Thursday, February 22, 2018 6:54 AM
using DBATools

$datatable = Import-Csv C:\temp\customers.csv | Out-DbaDataTable
Write-DbaDataTable -SqlServer sql2014 -InputObject $datatable -Table mydb.dbo.customers

Much better than foreach


Importing data from an existing file is not at all what they are doing though.
They are getting all services and then for each service, get the service name, status and display name.
But as the author explained, it's an example for using Invoke-SQLcmd.

Sue


Was showing the method, not the solution. Can be applied using any array or dataset.
So,
$var = Get-Service
Then use that as a parameter in the writedbatable commandlet.
Simplifies the process into 2 lines.

Catch-all queries done right
Gail Shaw's Performance Blog
Sue_H
Sue_H
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72640 Visits: 14937
MadAdmin - Thursday, February 22, 2018 8:43 AM
Sue_H - Thursday, February 22, 2018 7:32 AM
MadAdmin - Thursday, February 22, 2018 6:54 AM
using DBATools

$datatable = Import-Csv C:\temp\customers.csv | Out-DbaDataTable
Write-DbaDataTable -SqlServer sql2014 -InputObject $datatable -Table mydb.dbo.customers

Much better than foreach


Importing data from an existing file is not at all what they are doing though.
They are getting all services and then for each service, get the service name, status and display name.
But as the author explained, it's an example for using Invoke-SQLcmd.

Sue


Was showing the method, not the solution. Can be applied using any array or dataset.
So,
$var = Get-Service
Then use that as a parameter in the writedbatable commandlet.
Simplifies the process into 2 lines.


Yup..I get that and I use DBATools. Calling the function Write-DbaDataTable from DBATools which does the ForEach. So it's more a matter of using functions in Powershell. The ForEach isn't elimated, it's just used in the function.

Sue



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search