Im trying to automate a csv backup from a database i'm not adm. So i couldn't use most of the tools SQL Server dispose. The solution i was able to use is the following powershell code. It worked perfectly except for one specific table with a huge numeric (38 positions) field.
When i try to use the export-csv including the field, it returns the following error:
Exceção ao chamar "Fill" com "1" argumento(s): "Estouros de conversão."
No linha:29 caractere:5
$SqlAdapter.Fill($DataSet)
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : OverflowException
Im sorry about the error in Portuguese, but my computer is managed by my company, so i can't change the language. Im gonna try to translate it:
Exception calling "Fill" with "1" argument: "Conversion overflow."
Line: 29 character: 5
$SqlAdapter.Fill($DataSet)
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : OverflowException
I already was able to export it without the big number column or filtering the field like "< 99999999999999999999999999999". The type of the column is NUMERIC(38,0) and here is some examples of values that are causing the errors.
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"
}
My guess is the process is trying to convert the column to number and the value is bigger than powershell can handle. Could anyone help/save me? =)