Example on using array for INSERT operation with PostgreSQL #115
-
Hello, $Query = "INSERT INTO @table VALUES @data ON CONFLICT DO UPDATE;"
Invoke-SqlUpdate -Query $Query -Parameters @{table = $Params.TableName; data = $Data} I get an error:
How to do this mapping? Or is there another way? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@wplj -- that will not work. As far as I am aware, SQL does not support passing in the table name as a parameter. Moreover, SimplySql does not have any support for treating a CSV file or an array of PSObjects as a source for its Invoke-SqlBulkCopy. However, you can still get good performance through the use of transactions. $Query = "INSERT INTO theTable (col1, col2, col3) VALUES (@c1, @c2, @c3)"
Open-PostGreConnection # fill in connection details
Start-SqlTransaction
try {
foreach($d in $data) {
# setting the result to null is the equivalent of using '| Out-Null' but more performant
$null = Invoke-SqlUpdate -Query $Query -Parameters @{c1=$d.c1;c2=$d.c2;$c=$d.c3}
}
Complete-SqlTransaction
}
catch {
Undo-SqlTransaction # rollback on error
}
Close-SqlConnection |
Beta Was this translation helpful? Give feedback.
-
@wplj -- for valid SQL you will have to (as far as I am aware). FYI.. SimplySql (the current version) is untested/unsupported on PS7... so mileage may vary. As for the columnNames... if you have an object, you could dynamically generate them. |
Beta Was this translation helpful? Give feedback.
@wplj -- that will not work. As far as I am aware, SQL does not support passing in the table name as a parameter.
Moreover, SimplySql does not have any support for treating a CSV file or an array of PSObjects as a source for its Invoke-SqlBulkCopy.
However, you can still get good performance through the use of transactions.