Skip to content
Advertisement

Powershell foreach INSERT INTO SQL Server DB on multiple rows

I am trying to insert 2 separate arrays into multiple records on 1 SQL Insert Command. I have done the foreach command but it will only accept 1 of the arrays. I have tried doing nested foreach statements but that just puts in way to many records. Below is the code I have so far. I am not posting my connection code to my DB but I ensure you that it connecting to the DB.

What I am most confused about is how to make both arrays parse correctly into the DB. I hope I explained that correctly. Anything helps!

Here is an example of what it will need to look like:

This is what I want it to look like with Column 2 being the first array and column 3 being the second array. Column 1 will always be the same.

Advertisement

Answer

Based on the newly added expected result

Result is:

(Old solution) Based on your comments I think this is the result you want in your table:

This PS script generates the INSERT statement you need:

Result is:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement