我实现了导入几个Excel文件以更新数据库SQL Server中的表的脚本,但是运行脚本时出现错误:
invoke-sqlcmd:语法不正确,而不是'('。 AucaractèreC:\ Users \ WEBadmin \ Desktop \ updateDb.ps1:38:15 + ... $ impcsv = invoke-sqlcmd-数据库$ db_name-查询$ query -serverin ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~ + CategoryInfo:InvalidOperation:(:) [Invoke-Sqlcmd],SqlPowerShellSqlExecutionException + FullyQualifiedErrorId:SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
这是我的PS脚本:
$sql_instance_name = 'SERVER\SERV100'
$db_name = 'TOTO'
$table = 'dbo.F_ARTSTOCK'
$csv_folder = 'C:\IMPORTS'
$csv_completed_folder = 'C:\done'
$batchsize = 50000
# assuming only csv-files you want to import are located in $csv_folder
$csv_files = Get-Childitem -Path $csv_folder -Filter '*.csv'
# Build the sqlbulkcopy connection, and set the timeout to infinite
$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 $impcsv
$count = 1
foreach($i in $data) {
Write-Host ($data | Format-Table | Out-String)
$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'"
$impcsv = invoke-sqlcmd -Database $db_name -Query $query -serverinstance $sql_instance_name
write-host "Processing row ..........$count" -foregroundcolor green
$count = $count + 1
}
}
感谢您的帮助。