代码之家  ›  专栏  ›  技术社区  ›  mac10688

f#克隆记录数据

  •  2
  • mac10688  · 技术社区  · 9 年前

    我正在编写一个脚本,将数据从一个数据库服务器移动到另一个数据库。一次一张桌子对我来说很好。我把所有的外键等都去掉了。

    我在举这个例子。

    http://fsharpforfunandprofit.com/posts/low-risk-ways-to-use-fsharp-at-work-4/#sql-etl

    问题是我不想转换数据,我只想复制它。但是我有很多表,每个表都有很多列。什么是从一个记录映射到另一个记录而不映射每个字段的简单方法?

    1 回复  |  直到 9 年前
        1
  •  3
  •   jeremyh    9 年前

    您可以稍微修改@Petr引用的脚本,以复制指定的表,而不是所有的表。请参见下面的示例。

    // Start of modifications here
    
    // Create an array of objects in this case tables to copy
    let tablesToCopy = 
        sourceDatabase.Tables 
        |> Seq.cast<Table> // Convert TableCollection to seq<Table> so can use Seq functions
        |> Seq.filter (fun table -> [| "DimTime"; "DimCategory" |] |> Seq.exists (fun tableName -> tableName = table.Name)) 
        |> Seq.toArray // Materialise to an Array so it can be consumed by an ArrayList constructor and assigned to the ObjectList property
    
    let transferDatabase = 
        Transfer(sourceDatabase, 
            CopyAllObjects = false, // Set to false
            CopyAllSchemas = true, 
            CopyAllUserDefinedDataTypes = true, 
            CopyAllTables = false, // Set to false
            ObjectList = System.Collections.ArrayList(tablesToCopy), // Include a list of objects to copy - uses old style ArrayList
            CopyData = true, 
            CopyAllStoredProcedures = true,
            DestinationServer = destServer.Name,
            DestinationDatabase = destDatabase.Name)
    
    // End of modifications here