@Steven answer is good about using Here Strings make things simpler and easier to debug. In this case, it is not a PowerShell error that is happening. When you see "Msg 102, Level 15, State 1" that is a SQL error. You will have to check the schema of your database to ensure that you are passing in the right value. If you POID is a varchar type, then you may be simply missing a set of quotes around the VALUES that you are passing. e.g. :
...
values (
'$($row.PurchaseOrderID)',
4,
'Timeout waiting for mainframe to send response message',
getdate(),
'deb00535'
)
...
Remember, you are replacing $($row.PurchaseOrderID) with the value in the CSV. You still need the single quotes in the query for SQL to not complain ;-). So your code becomes:
$csv = Import-Csv 'D:\Chayan\POmiss\test.csv'
$SQLSERVER = "WSQL009D"
Foreach ($row in $csv) {
$query =
@"
INSERT
into BuyerWorksheetCentralRepository.[po].[PurchaseOrderMessage]
values (
'$($row.PurchaseOrderID)',
4,
'Timeout waiting for mainframe to send response message',
getdate(),
'deb00535'
)
"@
Invoke-Sqlcmd -ServerInstance $SQLSERVER -Database BuyerWorksheetCentralRepository -Query $query
}
If you need debugging, you can always do:
Write-Host $query
And see if that query will actually work in SQL.
The last thing that might be happening is to check your CSV file in Notepad. You may have an extra row of data that is blank. The error message eerily seems like you have one entry that has a blank value that is trying to be inserted.
EDIT
@ChayanChakraborty Provided the sample file:
PurchaseOrderID PONumMaster DivisionNum HoursElapsedSinceLastStatusUpdate
--------------- ----------- ----------- ---------------------------------
16601566 536958 8 2070
16601613 536998 8 1471
16601626 537011 8 700
Unfortunately @ChayanChakraborty, that is not a CSV file. Adding .CSV to the file name does not make it a Comma-Separated Values file. If you are exporting to file from Microsoft SQL Server Management Studio, you have to change the output options (see Here).
In Microsoft SQL Server Management Studio, go to Tools -> Options... menu. Then go to the Query Results -> SQL Server -> Results to Text section. Change the Output Format to Comma delimited, click Ok, and re-run your query. Your CSV file should only look like:
PurchaseOrderID,PONumMaster,DivisionNum,HoursElapsedSinceLastStatusUpdate
16601566,536958,8,2070
16601613,536998,8,1471
16601626,537011,8,700
You should verify that the end of the file also doesn't have the results as that will mess it up:
(3 rows affected)
Completion time: 2021-05-14T11:29:21.9260884-06:00