Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

huangapple go评论52阅读模式
英文:

how to drop table if exists in pre-copy script azure synapse pipeline Copy activity

问题

以下是要翻译的内容:

我有一个Azure Synapse 管道,如果目标表已经存在,它可以正常运行... 复制活动的预复制脚本是

@{concat('drop table ', item().target_schema, '.', item().target_object)}

但是我需要编辑上述语法,首先检查表是否存在,只有在存在的情况下才删除表... 下面是合法的 SQL Server 语法

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[myschema].[mytable]') AND type in (N'U'))
DROP TABLE [myschema].[mytable]
GO

正如您所看到的,我的预复制脚本是参数化的,因为我的 Azure Synapse Analytics 管道正在执行以下活动:

查找 - > ForEach - > 复制

因此,预复制脚本语法也必须是参数化的。如何实现IF EXISTS逻辑并将其放入参数化的预复制脚本语法中?

以下是预复制脚本的一些猜测... 所有都报错

if object_id (item().target_schema, '.', item().target_object,'U') is not null drop table item().target_schema, '.', item().target_object

以下失败

DROP TABLE IF EXISTS @{item().target_schema}.@{item().target_object}

出现错误:

"message": "ErrorCode=SqlOperationFailed,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=A database operation failed with the following error: 'Parse error at line: 1, column: 12: Incorrect syntax near 'IF'.',Source=,'Type=System.Data.SqlClient.SqlException,Message=Parse error at line: 1, column: 12: Incorrect syntax near 'IF'.,Source=.Net SqlClient Data Provider,SqlErrorNumber=103010,Class=16,ErrorCode=-2146232060,State=1,Errors=[{Class=16,Number=103010,State=1,Message=Parse error at line: 1, column: 12: Incorrect syntax near 'IF'.},],'"

英文:

I have an azure synapse pipeline which runs fine if target tables already exist ... Copy activity pre-copy script is

>@{concat('drop table ', item().target_schema, '.', item().target_object)}

however I need to edit above syntax to first check if table exists then only drop table if it exists ... below is legal sql server syntax

>IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[myschema].[mytable]') AND type in (N'U'))
DROP TABLE [myschema].[mytable]
GO

As you can see my pre-copy script is parameterized since my azure synapse analytics pipeline is doing these Activities :

Lookup -> ForEach -> Copy

so the pre-copy script syntax must also be parameterized

How do I implement the IF EXISTS logic yet put that into the parameterized pre-copy script syntax ?

The following guesses for the pre-copy script ... all error out

>if object_id (item().target_schema, '.', item().target_object,'U') is not null drop table item().target_schema, '.', item().target_object

below fails

DROP TABLE IF EXISTS @{item().target_schema}.@{item().target_object}

with error

>"message": "ErrorCode=SqlOperationFailed,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=A database operation failed with the following error: 'Parse error at line: 1, column: 12: Incorrect syntax near 'IF'.',Source=,''Type=System.Data.SqlClient.SqlException,Message=Parse error at line: 1, column: 12: Incorrect syntax near 'IF'.,Source=.Net SqlClient Data Provider,SqlErrorNumber=103010,Class=16,ErrorCode=-2146232060,State=1,Errors=[{Class=16,Number=103010,State=1,Message=Parse error at line: 1, column: 12: Incorrect syntax near 'IF'.,},],'",

答案1

得分: 1

这是我提供给ForEach的查找输出数组。

[
    {
        "table_name": "one",
        "schema_name": "dbo"
    },
    {
        "table_name": "two",
        "schema_name": "dbo"
    }
]

在复制活动的预复制脚本中,我使用了DROP TABLE IF EXISTS @{item().schema_name}.@{item().table_name}命令并获得了相同的错误。

Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

我在Synapse SQL数据库中尝试了相同的命令,结果也出现了相同的错误。

Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

根据此文档,这个错误的原因可能是上述命令只适用于SQL Server,而不适用于SQL DW

针对预复制脚本的以下猜测都会出错

如果 object_id (item().target_schema, '.', item().target_object,'U') 不为空则删除表 item().target_schema, '.', item().target_object

我可以通过使用上述命令的字符串插值在预复制脚本中删除表,如下所示。

if object_id ('@{item().schema_name}.@{item().table_name}','U') 不为空则删除表 @{item().schema_name}.@{item().table_name};

Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

英文:

This is my lookup output array which I have given to ForEach.

[
        {
            "table_name": "one",
            "schema_name": "dbo"
        },
        {
            "table_name": "two",
            "schema_name": "dbo"
        }
]

I reproduced the above scenario with DROP TABLE IF EXISTS @{item().schema_name}.@{item().table_name} command in pre-copy script of copy activity and got same error.

Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

I tried the same command in synapse SQL database and ended up with same error.

Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

As per this Documentation, the reason for this error might be the above command only applies to SQL Server and not SQL DW.

>The following guesses for the pre-copy script ... all error out
>
> if object_id (item().target_schema, '.', item().target_object,'U') is not null drop table item().target_schema, '.', item().target_object

I am able to drop the table in pre-copy script by using string interpolation with above command like below.

if object_id ('@{item().schema_name}.@{item().table_name}','U') is not null drop table @{item().schema_name}.@{item().table_name};

Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

Drop table if exists in pre-copy script Azure Synapse Pipeline Copy activity.

huangapple
  • 本文由 发表于 2023年4月13日 23:53:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定