I'm doing a script to import CSVs (commands) that update the stock in SQL Server. But I encounter this error when I run the following script:
Invoke-Sqlcmd : La conversion de la valeur varchar '2200100001' a dépassé une colonne int. L'instruction a été arrêtée. Au caractère C:\\Users\\admin\\Desktop\\updateDb.ps1:36 : 19 + ... $impcsv = Invoke-Sqlcmd -Database $db_name -Query $query -ServerIns ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation : (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Here is my powershell script:
$sql_instance_name = 'SERVER\SAGE100'
$db_name = 'SERVER_DB'
$table = "dbo.F_ARTSTOCK"
$csv_folder = 'F:\EUR'
$csv_completed_folder = 'F:\EUR\done'
$batchsize = 50000
$csv_files = Get-Childitem -Path $csv_folder -Filter '*.csv'
$connectionstring = "Data Source=$sql_instance_name;Integrated Security=true;Initial Catalog=$db_name;"
$bulkcopy = New-Object Data.SqlClient.SqlBulkCopy($connectionstring, [System.Data.SqlClient.SqlBulkCopyOptions]::TableLock)
$bulkcopy.DestinationTableName = $table
$bulkcopy.bulkcopyTimeout = 0
$bulkcopy.batchsize = $batchsize
foreach($file in $csv_files) {
$impcsv = $file.FullName
write-host "Processing file ..........$impcsv" -foregroundcolor green
$data = import-csv -Delimiter ";" $impcsv
$count = 1
foreach($i in $data) {
Write-Host $i.Reference
$reference = $i.Reference
$quantity = $i.Quantity
$query = "UPDATE dbo.F_ARTSTOCK (AR_Ref,AS_QteSto)
SET dbo.F_ARTSTOCK.AS_QteSto = dbo.F_ARTSTOCK.AS_QteSto - $quantity
WHERE dbo.F_ARTSTOCK.AR_Ref = $reference"
Write-Host $query
$impcsv = invoke-sqlcmd -Database $db_name -Query $query -ServerInstance $sql_instance_name
write-host "Processing row ..........$count" -foregroundcolor green
$count = $count + 1
}
}
Do you have any idea where the problem would come from?
Thank you for your help.
The error message is in french. Translated it reads: The conversion of the '2200100001' varchar value has exceeded an int column. The instruction has been stopped.
Basically what that means is you have a 32-bit int column on that table and it needs to be of bigint type to handle a larger value... Or go through the data that you're trying to submit and verify that it is accurate (aka: remove that row from the csv. Depending on how sensitive of data you are working on).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.