Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DROP TABLE IF EXISTS not working on Azure SQL Data Warehouse

I used the SQL Server management studio to generate script against Azure Data Warehouse. I selected Edition Azure Data Warehouse, it generates below the script to drop table if it exists and create table. However, the script cannot pass validation. Please see below for the error message.

DROP TABLE IF EXISTS Table1
GO

Error message:

Parse error at line: 2, column: 12: Incorrect syntax near 'IF'.

like image 486
Edison Avatar asked Feb 13 '18 23:02

Edison


People also ask

What happens on drop if exist and the table does not exist?

The DROP TABLE statement deletes the specified table, and any data associated with it, from the database. The IF EXISTS clause allows the statement to succeed even if the specified tables does not exist. If the table does not exist and you do not include the IF EXISTS clause, the statement will return an error.

How do I drop a table in SQL Azure?

To drop a table from within the current database (on-premises or Azure SQL Database) we simply pass the name of the table in the form [schema_name]. [table_name]. As before, we get an error that shows us the table is no longer there trying when we try to select from it.

Why can't I DROP TABLE in SQL?

The reason SQL won't let you drop a table in this situation is because the allocation pages/extent chain appears to be damaged or cross-linked in some way. So SQL Server thinks that there is actually data from other tables in pages/extents belonging to the problem object.


2 Answers

As indicated in the documentation, DROP TABLE IF EXISTS is not currently supported in Azure SQL Data Warehouse, as at today:

-- Syntax for SQL Server and Azure SQL Database  
DROP TABLE [ IF EXISTS ] [ database_name . [ schema_name ] . | schema_name . ]  
table_name [ ,...n ]  
[ ; ]

-- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse    
DROP TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name   
[;] 
like image 112
wBob Avatar answered Oct 26 '22 22:10

wBob


Bob's answer is correct. I tried to add the following as a comment, but it looked terrible without formatting.

You can use the following syntax as an alternative:

if object_id ('dw.dim_location','U') is not null drop table dw.dim_location;
like image 24
Ron Dunn Avatar answered Oct 26 '22 21:10

Ron Dunn