Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use SMO to only transfer one table from one DB to another. Instead, the entire DB is copied. Using "CopyAllTables = False" does not seem to work.

The code for the method I developed is:

' copy a data table from one db to another
Public Sub CopyTable(ByVal iSourceServerName As String,
                        ByVal iTargetServerName As String,
                        ByVal iSrcDatabase As String,
                        ByVal iTargerDatabase As String,
                        ByVal iTblName As String,
                        <Out> ByRef iRetMsg As String)

    Dim srcsrv = New Server(iSourceServerName)
    Dim srcdb As Database = srcsrv.Databases(iSrcDatabase)
    Dim iTbl = srcdb.Tables(iTblName) ' the table to transfer
    Dim targetsrv = New Server(iTargetServerName)
    Dim targetdb As New Database(targetsrv, iTargerDatabase)

    Dim trans As New Transfer(srcdb)
    With trans
        .DestinationDatabase = iTargerDatabase
        .DestinationServer = iTargetServerName
        .DestinationLoginSecure = True
        .ObjectList.Add(iTbl) ' only transfer the specified table
        .CopyAllTables = False
        .CopySchema = True

        With .Options
            .ContinueScriptingOnError = True
            .DriAllKeys = True
            .WithDependencies = False
            .DriAll = False
            .DriDefaults = True
            .DriIndexes = True
            .DriPrimaryKey = True
            .DriUniqueKeys = True
            .DriForeignKeys = False
        End With

    End With

    Try
        trans.TransferData()

    Catch ex As Exception
        iRetMsg = ex.ToString

    End Try


This assumes the destination DB already exists.

The transfer throws an error indicating that the target DB does not have enough storage allocated.

What I have tried:

I have tried searching on-line for reporting of this problem but have not found anything.
Posted
Updated 5-Feb-21 9:22am
Comments
CHill60 5-Feb-21 4:16am    
Only things that come to mind are
- are the 'other' tables populated or just the schema copied across? I'm looking at the .CopySchema = True
- What if you try using the parameterless constructor for New Transfer()
Kevin Brady 5-Feb-21 10:53am    
The tables that transfer are not populated with data. Only the schema transfers.

Not all of the tables are copied from the source to the target because the transfer throws an error that has to do with the size of the target (another issue for another time).

I'm not sure what you mean by "parameterless constructor" for the transfer. Please explain. Do you mean NOT passing the srcdb?
Kevin Brady 5-Feb-21 15:19pm    
I have set the transfer parameter "CopyAllObjects = False". Now, only the desired table transferred. This seems to be the important parameter for the desired functionality.

Also, if I set "CopyData = True" the data transfers along with the table.

The revised complete code is now:

' copy a data table from one db to another
Public Sub CopyTable(ByVal iSourceServerName As String,
ByVal iTargetServerName As String,
ByVal iSrcDatabase As String,
ByVal iTargerDatabase As String,
ByVal iTblName As String,
ByVal CopyData As Boolean,
<out> ByRef iRetMsg As String)

Dim srcsrv = New Server(iSourceServerName)
Dim srcdb As Database = srcsrv.Databases(iSrcDatabase)
Dim iTbl = srcdb.Tables(iTblName) ' the table to transfer

Dim targetsrv = New Server(iTargetServerName)
Dim targetdb As New Database(targetsrv, iTargerDatabase)

Dim trans As New Transfer()
With trans
.Database = srcdb

.DestinationServer = iTargetServerName
.DestinationDatabase = iTargerDatabase
.DestinationLoginSecure = True
.ObjectList.Add(iTbl) ' only transfer the specified table
.CopyAllObjects = False
.CopyAllTables = False
.CopySchema = CopyData

.CopyData = True

With .Options
.ContinueScriptingOnError = True
.DriAllKeys = True
.WithDependencies = False
.DriAll = False
.DriDefaults = True
.DriIndexes = True
.DriPrimaryKey = True
.DriUniqueKeys = True
.DriForeignKeys = False
End With

End With

Try
trans.TransferData()

Catch ex As Exception
iRetMsg = ex.ToString

End Try

End Sub

1 solution

The revised code for the method is:

' copy a data table from one db to another
Public Sub CopyTable(ByVal iSourceServerName As String,
                        ByVal iTargetServerName As String,
                        ByVal iSrcDatabase As String,
                        ByVal iTargerDatabase As String,
                        ByVal iTblName As String,
                        ByVal CopyData As Boolean,
                        <Out> ByRef iRetMsg As String)

    Dim srcsrv = New Server(iSourceServerName)
    Dim srcdb As Database = srcsrv.Databases(iSrcDatabase)
    Dim iTbl = srcdb.Tables(iTblName) ' the table to transfer

    Dim targetsrv = New Server(iTargetServerName)
    Dim targetdb As New Database(targetsrv, iTargerDatabase)

    Dim trans As New Transfer()
    With trans
        .Database = srcdb

        .DestinationServer = iTargetServerName
        .DestinationDatabase = iTargerDatabase
        .DestinationLoginSecure = True
        .ObjectList.Add(iTbl) ' only transfer the specified table
        .CopyAllObjects = False
        .CopyAllTables = False
        .CopySchema = True

        .CopyData = CopyData

        With .Options
            .ContinueScriptingOnError = True
            .DriAllKeys = True
            .WithDependencies = False
            .DriAll = False
            .DriDefaults = True
            .DriIndexes = True
            .DriPrimaryKey = True
            .DriUniqueKeys = True
            .DriForeignKeys = False
        End With

    End With

    Try
        trans.TransferData()

    Catch ex As Exception
        iRetMsg = ex.ToString

    End Try

End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900