【发布时间】:2021-01-16 14:49:55
【问题描述】:
我正在尝试从我不是 adm 的数据库中自动执行 csv 备份。所以我不能使用 SQL Server 处理的大多数工具。 我能够使用的解决方案是以下 powershell 代码。 除了一个具有巨大数字(38 个位置)字段的特定表之外,它运行良好。
当我尝试使用包含该字段的 export-csv 时,它返回以下错误:
Exceção ao chamar "Fill" com "1" argumento(s): "Estouros de conversão."
没有 linha:29 字符:5
$SqlAdapter.Fill($DataSet)
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : OverflowException
对于葡萄牙语的错误,我很抱歉,但我的电脑是由我公司管理的,所以我无法更改语言。我试着翻译一下:
使用“1”参数调用“填充”的异常:“转换溢出。”
行:29 个字符:5
$SqlAdapter.Fill($DataSet)
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : OverflowException
我已经能够在没有大数字列或过滤“
9332003150820027276792001711182003
143295882003819000120030011493142
1411110005678200117658820118210141
1205000040601104009022001104009396
#Variables
$SQLServer = "myserver"
$SQLDBName = "myDB"
$SQLSchema = "Schema"
$delimiter = "¬"
$encoding = "UTF8"
$UseQuotes = "Always"
$tabelas = @("myTable")
#Conexão SQL
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConnection
foreach ($tab in $tabelas) {
#Comandos SQL
$SqlQuery = "SELECT * from [$SQLDBName].[$SQLSchema].[$tab];"
$SqlCmd.CommandText = $SqlQuery
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.SelectCommand.CommandTimeout = 2400;
#Exporta dados
$hour = Get-Date -Format HH:mm:ss
"Começou o export da tabela $tab às $hour"
Get-Date -Format HH:mm:ss
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | export-csv -Delimiter $delimiter -Encoding $encoding -Path "C:\exports\$SQLSchema.$tab.csv" -NoTypeInformation
$hour = Get-Date -Format HH:mm:ss
"Terminou o export da tabela $tab às $hour"
}
我的猜测是该过程正在尝试将列转换为数字,并且该值大于 powershell 可以处理的值。任何人都可以帮助/拯救我吗? =)
【问题讨论】:
-
不幸的是,这似乎是设计使然。 .NET 的十进制结构的最大比例为 28。 SQL 的最大值为 38。
标签: sql-server powershell csv export