SSIS 2016 SMO Transfer error: An error occured while transferring data. See inner exception for details

341 views Asked by At

We are using SMO to transfer tables over from one server to the other on SQL Server 2016.

For a while now, the transfer succeeds only one out of 3 times on 1 server only, and we always get the same error message, which doesn't say much: An error occured while transferring data. See inner exception for details.

That same package has been deployed to our 4 environments (Dev, Test, Qa and Prod) and is running everyday. However, we only get that problem in Test.

Our DBAs are not doing much to help us find the reason why that appends. Can you please give me a hint on where to look at? That SSIS package hasn't been updated for a long time, and the built version is the same in all environment.

Here is our .vb script in the package:

Public Sub Main()
    '
    ' Source and Destination Servers
    '
    ' MsgBox(Dts.Variables("User::SourceServer").Value.ToString)
    Dim srvSourceName As String = Dts.Variables("SourceServer").Value.ToString  '"brdsqldev"
    Dim srvDestinationName As String = Dts.Variables("DestinationServer").Value.ToString  '"brdsqldev"
    Dim bt(0) As Byte
    '
    ' Source and Destination Databases
    '
    Dim dbSourceName As String = Dts.Variables("SourceDB").Value.ToString
    Dim dbDestinationName As String = Dts.Variables("DestinationDB").Value.ToString

    Try
        Dts.Log("The Transfer starts", 0, bt)
        Dts.Log("Transfer starts with tables", 0, bt)
        Dts.Log("Transfer starts with tables" & srvSourceName, 0, bt)
        Dts.Log("Transfer starts with tables" & srvDestinationName, 0, bt)
        Dts.Log("Transfer starts with tables" & dbSourceName, 0, bt)
        Dts.Log("Transfer starts with tables" & dbDestinationName, 0, bt)
        ' TableExists()
        Transfer(srvSourceName, srvDestinationName, dbSourceName, dbDestinationName, CType(Dts.Variables.Item("myDataTable").Value, System.Data.DataTable), TransferObjectType.Table)
        ' Transfer(srvSourceName, srvDestinationName, dbSourceName, dbDestinationName, CType(Dts.Variables.Item("myDataViews").Value, System.Data.DataTable), TransferObjectType.View)
        Dts.Log("Transfer done with tables", 0, bt)
        Dts.Log("The Transfer ran successfully", 0, bt)
        Dts.TaskResult = ScriptResults.Success
        'MessageBox.Show("The transfer ran successfully.")

    Catch ex As Exception
        Dts.Events.FireError(99, "", ex.Message.ToString(), "", -1)
        OutPutError(ex)
        Dts.TaskResult = ScriptResults.Failure
        'MessageBox.Show("The transfer was aborted.")
    End Try

End Sub


Private Sub Transfer(ByVal srvSourceName As String, ByVal srvDestinationName As String, ByVal dbSourceName As String, ByVal dbDestinationName As String, ByVal dataTableToTransfer As DataTable, ByVal type As TransferObjectType)
    Try
        Dim srcsrv As Server
        srcsrv = New Server(srvSourceName)

        Dim dbSource As Database
        dbSource = srcsrv.Databases(dbSourceName)

        Dim dessrv As Server
        dessrv = New Server(srvDestinationName)

        Dim dbDestination As Database
        dbDestination = dessrv.Databases(dbDestinationName)

        Dim oleDA As New OleDbDataAdapter
        Dim dt As New DataTable
        Dim row As DataRow
        Dim sMsg As String

        Dim xfr As Transfer

        Dim bt(0) As Byte

        Dts.Log("Transfer Subroutine starts ...", 0, bt)
        xfr = New Transfer(dbSource)

        xfr.CopyAllTables = False
        xfr.Options.WithDependencies = False
        xfr.Options.ContinueScriptingOnError = False

        xfr.CopyAllRoles = False
        xfr.CopyAllLogins = False
        xfr.CopyAllDatabaseTriggers = False
        xfr.CopyAllDefaults = False
        xfr.CopyAllPartitionFunctions = False
        xfr.CopyAllObjects = False
        xfr.CopyAllPartitionFunctions = False
        xfr.CopyAllPartitionSchemes = False
        xfr.CopyAllRules = False
        xfr.CopyAllSchemas = False
        xfr.CopyAllSqlAssemblies = False
        xfr.CopyAllStoredProcedures = False
        xfr.CopyAllSynonyms = False
        xfr.CopyAllUserDefinedAggregates = False
        xfr.CopyAllUserDefinedDataTypes = False
        xfr.CopyAllUserDefinedFunctions = False
        xfr.CopyAllUserDefinedTypes = False
        xfr.CopyAllUsers = False
        xfr.CopyAllViews = False
        xfr.CopyAllXmlSchemaCollections = False
        xfr.CopySchema = True

        xfr.DestinationDatabase = dbDestination.Name
        xfr.DestinationServer = dessrv.Name
        xfr.DestinationLoginSecure = True
        xfr.DropDestinationObjectsFirst = False

        Select Case type
            Case TransferObjectType.Table
                xfr.CopyData = True
            Case TransferObjectType.View
                xfr.CopyData = False
            Case Else
                Dts.Log("Unknown object transfer type. (-99)", -99, bt)
                Dts.TaskResult = ScriptResults.Failure
                'Throw New Exception("Unknown object transfer type.")
        End Select

        dt = dataTableToTransfer
        Dts.Log("Transfer Subroutine just before Row Count...", 0, bt)
        If dt.Rows.Count > 0 Then
            Dts.Log("Transfer Row Count > 0...", 0, bt)
            For Each row In dt.Rows
                Dim tblSourceName As String
                Dim tblSourceSchema As String

                tblSourceSchema = row("transferschema").ToString()
                tblSourceName = row("transferobject").ToString

                Select Case type
                    Case TransferObjectType.Table
                        xfr.ObjectList.Add(dbSource.Tables(tblSourceName.ToString, tblSourceSchema.ToString))
                    Case TransferObjectType.View
                        xfr.ObjectList.Add(dbSource.Views(tblSourceName.ToString, tblSourceSchema.ToString))
                    Case Else
                        Dts.Events.FireError(99, "Transfer Object", "Unknows object type", "", -1)
                        Dts.TaskResult = ScriptResults.Failure
                        'Throw New Exception("Unknown object transfer type.")
                End Select

            Next
            Dts.Events.FireInformation(1, "", "Just before transfering data", "", -1, False)

            xfr.TransferData()

        End If

    Catch ex As InternalSmoErrorException
        Dim bt(0) As Byte
        Dts.Events.FireError(99, "Transfer Objects", ex.Message.ToString(), "", -1)
        OutPutError(ex)
        Dts.TaskResult = ScriptResults.Failure

    End Try

End Sub

Private Sub OutPutError(ex As Exception)
    Dim ErrorLogDir As String
    Dim OutPutFileName As String
    ErrorLogDir = System.IO.Path.GetDirectoryName(Dts.Connections("ExportNightlyTransfer.xml").ConnectionString.ToString())
    OutPutFileName = ErrorLogDir + "\\" + Dts.Variables("PackageName").Value.ToString() + "ErrorReport" + Now.ToString("yyyyMMddHHmmss") + ".txt"
    Using sw As StreamWriter = New StreamWriter(OutPutFileName)
        sw.WriteLine(Now.ToString())
        sw.WriteLine("Package Name: " + Dts.Variables("PackageName").Value.ToString())
        sw.WriteLine("Task Name: " + Dts.Variables("TaskName").Value.ToString())
        sw.WriteLine(ex.Message.ToString())
        sw.WriteLine(ex.GetBaseException.ToString())
        sw.WriteLine(ex.StackTrace.ToString())
    End Using
End Sub

Thanks in advance for your help. Mylene

1

There are 1 answers

0
MChalut On

By adding the Catch innerException code, I was able to get specific information about the error.

  Catch ex As Exception
  Dts.Events.FireError(99, "", ex.Message.ToString(), "", -1)
  OutPutError(ex)

  If ex.InnerException IsNot Nothing Then
    Dts.Events.FireError(99, "", ex.InnerException.Message.ToString(), "", -1)
    OutPutError(ex.InnerException.Message.ToString())
  End If


  Dts.TaskResult = ScriptResults.Failure
  'MessageBox.Show("The transfer was aborted.")
End Try

Problem solved!! Thank you :-)