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 ?