How to drop table if exists in synapse pipeline pre copy script ?

sekhemrekhutawysobekhotep 46 Reputation points
2023-04-06T14:07:16.02+00:00

I have a synapse pipeline with a Copy activity doing a "Bulk insert" so need to drop table ... this syntax works ONLY IF TABLE already exists @{concat('drop table ', item().target_schema, '.', item().target_object)} naturally first time I run this pipeline target table does NOT exist so pipeline fails I need to fix above to use functional equivalent to DROP TABLE IF EXISTS
however that syntax fails in azure synapse with error https://stackoverflow.com/questions/48777206/drop-table-if-exists-not-working-on-azure-sql-data-warehouse I have tried many many possible syntax as per


@{concat('IF EXISTS (SELECT [name] FROM sys.tables WHERE [name] like ', item().target_schema, '.', item().target_object,')
 BEGIN
   DROP TABLE ', item().target_schema, '.', item().target_object, ';
 END; '
)}



which errors with

{
    "errorCode": "2200",
    "message": "ErrorCode=SqlOperationFailed,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=A database operation failed with the following error: 'The multi-part identifier \"myschema.mytable\" could not be bound.',Source=,''Type=System.Data.SqlClient.SqlException,Message=The multi-part identifier \"myschema.mytable\" could not be bound.,Source=.Net SqlClient Data Provider,SqlErrorNumber=4104,Class=16,ErrorCode=-2146232060,State=1,Errors=[{Class=16,Number=4104,State=1,Message=The multi-part identifier \"myschema.mytable\" could not be bound.,},],'",
    "failureType": "UserError",
    "target": "Copy data1",
    "details": []
}

also tried so for other SQL this is appropriate

IF OBJECT_ID('dbo.Scores', 'U') IS NOT NULL 
  DROP TABLE dbo.Scores; 

however my below translation of above into synapse pre copy script syntax errors


@{concat('IF  OBJECT_ID(', item().target_schema, '.', item().target_object, ', \'U\') IS NOT NULL
   DROP TABLE ', item().target_schema, '.', item().target_object, ';'
)}

this also errors out


@{concat('IF  OBJECT_ID(', item().target_schema, '.', item().target_object, ', "U") IS NOT NULL
   DROP TABLE ', item().target_schema, '.', item().target_object, ';'
)}


below also errors out

@{concat("IF  OBJECT_ID(", item().target_schema, ".", item().target_object, ", 'U') IS NOT NULL
   DROP TABLE ", item().target_schema, ".", item().target_object, ";"
)}


Has anyone managed to run a Copy activity pre copy script to drop table IF exists ?

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,373 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. HimanshuSinha-msft 19,486 Reputation points Microsoft Employee Moderator
    2023-04-07T22:35:29.9133333+00:00

    Hello @sekhemrekhutawysobekhotep , Thanks for the question and using MS Q&A platform. When you are changing the TSQL script like this

    if object_id ('dw.dim_location','U') is not null drop table dw.dim_location;

    Please keep in mind that ' also needs to be accounted for, I am trying to highlight the same in the below User's image

    I think the attched expression should help you out . EXPRESSION.TXT There should be NO "new line" character I know at times I struggle with dynamic expression and what I do is I will try and use the SET variable activity and check the expression there. Once I have the expression I will copy and run the same in SSMS and cross check it. User's image

    Thanks
    Himanshu
    Please accept as "Yes" if the answer provided is useful , so that you can help others in the community looking for remediation for similar issues. 


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.