Sql server 使用Azure Data Factory中的复制数据工具指定要复制的表的顺序以尊重外键

Sql server 使用Azure Data Factory中的复制数据工具指定要复制的表的顺序以尊重外键,sql-server,azure,azure-sql-database,azure-data-factory,azure-data-factory-2,Sql Server,Azure,Azure Sql Database,Azure Data Factory,Azure Data Factory 2,该工具允许我选择要从源复制到目标的表,但表是按字母顺序复制的。因为我定义了外键,所以这无法工作。我想手动更改顺序。不幸的是,据我所知,只能以默认顺序传输表。它无法扫描约束策略并以最佳顺序执行 因此,恐怕您必须弄清楚您的约束策略是如何设置的,并手动制定正确的顺序。在获得表名排序列表后,为每个表逐个创建复制活动 当然,不要担心这一部分。每个元素都可以由或创建。您只需循环列表并将其传递到代码或脚本片段中。每个活动只需更改表名。这里有一个简单的管道,可以将数据从一个数据库复制到另一个数据库-它有一个Fo

该工具允许我选择要从源复制到目标的表,但表是按字母顺序复制的。因为我定义了外键,所以这无法工作。我想手动更改顺序。

不幸的是,据我所知,只能以默认顺序传输表。它无法扫描约束策略并以最佳顺序执行

因此,恐怕您必须弄清楚您的约束策略是如何设置的,并手动制定正确的顺序。在获得表名排序列表后,为每个表逐个创建复制活动


当然,不要担心这一部分。每个元素都可以由或创建。您只需循环列表并将其传递到代码或脚本片段中。每个活动只需更改表名。

这里有一个简单的管道,可以将数据从一个数据库复制到另一个数据库-它有一个ForEach,其中包含两个活动。它复制每个表,然后在每个表之后运行存储过程。表中的列名相同。它有一个名为tableMapping的变量,这是一个json数组,定义了“from”和“to”表名,因为它们可能不同。ForEach具有此设置“batchCount”:1,因此它一次运行一个。在我刚才的测试中,它按照变量tableMapping中的顺序处理表。如果不指定batchCount=1,那么它将并行运行它们

{
    "name": "TowWorks to Azure SQL DB",
    "properties": {
        "activities": [
            {
                "name": "ForEach1",
                "type": "ForEach",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "items": {
                        "value": "@variables('tableMapping')",
                        "type": "Expression"
                    },
                    "batchCount": 1,
                    "activities": [
                        {
                            "name": "Copy from BI to Azure",
                            "type": "Copy",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "SqlServerSource",
                                    "queryTimeout": "02:00:00"
                                },
                                "sink": {
                                    "type": "AzureSqlSink",
                                    "preCopyScript": {
                                        "value": "@{concat('truncate table raw.', item().to)}",
                                        "type": "Expression"
                                    },
                                    "disableMetricsCollection": false
                                },
                                "enableStaging": false
                            },
                            "inputs": [
                                {
                                    "referenceName": "BI_TW_Raw_NoTable",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "tableName": {
                                            "value": "@item().from",
                                            "type": "Expression"
                                        }
                                    }
                                }
                            ],
                            "outputs": [
                                {
                                    "referenceName": "TW_Azure_DB_noTable",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "schema": "raw",
                                        "table": {
                                            "value": "@item().to",
                                            "type": "Expression"
                                        }
                                    }
                                }
                            ]
                        },
                        {
                            "name": "Stored Procedure1",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "Copy from BI to Azure",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": {
                                    "value": "@concat('raw.Update', item().to)",
                                    "type": "Expression"
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "TowWorksAzureSqlDB",
                                "type": "LinkedServiceReference"
                            }
                        }
                    ]
                }
            }
        ],
        "variables": {
            "tableMapping": {
                "type": "Array",
                "defaultValue": [
                    {
                        "from": "WORK_TASKS",
                        "to": "WorkTask"
                    },
                    {
                        "from": "Invoice",
                        "to": "Invoice"
                    },
                    {
                        "from": "ASSET",
                        "to": "Asset"
                    },
                    {
                        "from": "InvoiceDistribution",
                        "to": "InvoiceDistribution"
                    },
                    {
                        "from": "InvoiceEvent",
                        "to": "InvoiceEvent"
                    },
                    {
                        "from": "InvoiceTransaction",
                        "to": "InvoiceTransaction"
                    },
                    {
                        "from": "LOGISTICS_ORDERS",
                        "to": "LogisticOrder"
                    },
                    {
                        "from": "LOGISTICS_ORDERS_LINE_ITEMS",
                        "to": "LogisticOrderLineItem"
                    },
                    {
                        "from": "WORK_ORDERS",
                        "to": "WorkOrder"
                    },
                    {
                        "from": "WORK_ORDERS_STATUSES",
                        "to": "WorkOrderStatus"
                    }
                ]
            },
            "from": {
                "type": "String"
            },
            "to": {
                "type": "String"
            }
        },
        "folder": {
            "name": "TowWorks"
        },
        "annotations": []
    }
}

我还没有使用这个工具本身,但它看起来像是为您创建管道。您能否编辑这些管道以更改或强制执行订单?我已经创建了管道,其中我为它提供了一个名称数组和一个For-Each,其中包含一个副本-还没有测试序列,但它似乎是按照数组的顺序执行的。@ScottMildenberger是的,它确实创建了一个管道。有一个“cw_items”参数,它包含一个最小化的300行JSON,其中我有4个表,所有映射都指向它,我可以在那里更改顺序,但这非常痛苦!显然,这不是一个可扩展的解决方案。