Skip to content

Example on using array for INSERT operation with PostgreSQL #115

Closed Answered by mithrandyr
wplj asked this question in Q&A
Discussion options

You must be logged in to vote

@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…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@wplj
Comment options

Answer selected by mithrandyr
Comment options

You must be logged in to vote
1 reply
@wplj
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants