Not Getting Back a Count from an Insert Executed by ExecuteNonQuery Against MS SQL Server from PowerShell script

0

The following code executes a BULK INSERT which succeeds, but it does not return the number of rows affected. When I do a 'SELECT count(*) ...' to confirm the count, millions of rows have been inserted. I suspect the overflow error is part of the problem. Why is the rows affected count failing to return? Any ideas of how to get the count? I have several other similar routines and they always return the count.

Code

try {

     $Command = New-Object System.Data.SQLClient.SQLCommand
     $Command.Connection = "Persist Security Info=False;Integrated Security=true;Initial Catalog=$($file_info.database);server=$($file_info.db_server_instance)"
     $Command.CommandTimeout = 18000 # 5 hours
     $Command.Connection.Open()

     try {

          $bulkinsert_sql = "BULK INSERT $($file_info.database).$($file_info.owner).$($file_info.table) FROM '$load_file_path' with (ERRORFILE='$load_file_error_path', MAXERRORS=$($file_info.dbload_error_max), TABLOCK, FIELDTERMINATOR='\t')"
          $Command.CommandText = $bulkinsert_sql
          $loaded_cnt=$Command.ExecuteNonQuery()
          write-host "$loaded_cnt ROWS LOADED"

     } catch [System.Data.SqlClient.SqlException] {

          $_ | select -expandproperty invocationinfo | Format-List Line, PositionMessage -force
          write-host $_.Exception.ToString() -foregroundcolor "red"     
          $ErrorActionPreference = "Continue"
     }

     # set error preference back
     $ErrorActionPreference = "stop"

} catch [System.Data.SqlClient.SqlException] {

     $_ | select -expandproperty invocationinfo | Format-List Line, PositionMessage -force
     write-host $_.Exception.ToString() -foregroundcolor "red"
     $ErrorActionPreference = "stop"

} finally {

     if ($Command.Connection -eq 'Open') {
         $Command.Connection.Close()
     }
}

Error Messages Caught

System.Data.SqlClient.SqlException (0x80131904): Bulk load data conversion error (truncation) for row 1714271, column 72 (PROVIDER_NAME1).
Bulk load data conversion error (truncation) for row 1714272, column 72 (PROVIDER_NAME1).
Bulk load data conversion error (truncation) for row 2083378, column 72 (PROVIDER_NAME1).
Bulk load data conversion error (overflow) for row 4633809, column 82 (TF_ME_TOT_DEDUCTIONS).
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, 
TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean&
 usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at CallSite.Target(Closure , CallSite , Object )
ClientConnectionId:ba9e07ac-1bc4-4c59-8a0a-167d7b281644
Error Number:4863,State:1,Class:16
sql-server
powershell
error-handling
executenonquery
asked on Stack Overflow Jan 3, 2020 by Mark • edited Jan 3, 2020 by Mark

1 Answer

1

Why is the rows affected count failing to return? Any ideas of how to get the count?

ExecuteNonQuery is not the "lowest level" method for running a query. It makes some simplifying assumptions about what information SQL Server might return.

When running a query from the client SQL Server can return any combination of Error messages, Row-count messages, and result sets. But to simplify things for the normal case, SqlClient throws a SqlException when it sees an error message, and so ExecuteNonQuery doesn't even return (let alone return a rowcount).

If you want ExecuteNonQuery to return, you can redirect all error messages to an event handler, and supress the .NET Exception throwing behavior with SqlConnection.FireInfoMessageEventOnUserErrors.


User contributions licensed under CC BY-SA 3.0