MSSQL lets you do a multi-row UPDATE and INSERT from some source, like another table, or JSON & XML data, etc. Psuedo-example:
INSERT INTO TBL SELECT * FROM SOURCE
Is it possible to use a PowerShell hash table as the source?
#psuedo code
$carHashTable = [pscustomobject]@{
color = 'red'
make = 'geo'
model = 'metro'
year = 1996
insured = 0
}
#here-string
$sql = @"
insert into
SomeDumbTable
select
*
from $carHashTable
"@
Invoke-Sqlcmd -Query $sql
All the examples I've seen online use a foreach loop to do the actual insert or update. Bossmang says loops (in SQL proper) are bad.
The only other option I can think of is using PowerShell to create a temp table in SQL, then use the temp table as the source to do the the multi-insert.
Import-Module SqlServerandGet-Help Write-SqlTableData -Full?$param.TypeNamewrite-SqlTableDatais AMAZAING; unfortunately though, it does not support#temptables. I want to do this without using a Stored Procedure; however, it is an option, and that article helps. I even triedSqlBulkCopybut that didn't work, either$bulkCopy.DestinationTableName = "tempdb..#Test187". "Loops are bad" in SQL, but not in PowerShell, I guess? I'm going to have to do aforeachwith anupdateper record. It doesn't seem like PowerShell has a SQLupdatecmdlet yet, or the option to convert objects into DataTables.