简体   繁体   English

脚本Powershell导入多个CSV

[英]Script powershell import multiple CSV

I'm doing a script to import CSVs (commands) that update the stock in SQL Server. 我正在执行一个脚本来导入CSV(命令),以更新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. Invoke-Sqlcmd:从valchar varchar转换为'2200100001',这是一个重复的殖民地int。 L'instruction a été arrêtée. 指令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 AuCactèreC:\\ Users \\ admin \\ Desktop \\ updateDb.ps1:36:19 + ... $ impcsv =调用SQLcmd-数据库$ db_name-查询$ query -ServerIns ... + ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(:) [Invoke-Sqlcmd],SqlPowerShellSqlExecutionException + FullyQualifiedErrorId:SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

Here is my powershell script: 这是我的powershell脚本:

$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. 翻译为:'2200100001'varchar值的转换已超过int列。 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). 基本上,这意味着您在该表上具有32位int列,并且该列必须为bigint类型才能处理更大的值...或遍历您尝试提交的数据并验证其是否正确(又名:从csv中删除该行。具体取决于您对数据的敏感程度)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM