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'.,},],'",