cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

ADD COLUMN IF NOT EXISTS does not recognize "IF NOT EXIST". How do I add a column to an existing delta table with SQL if the column does not already exist?

Christine
Contributor

How do I add a column to an existing delta table with SQL if the column does not already exist?

I am using the following code:

<%sql

ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name type; >

but it prints the error:

<[PARSE_SYNTAX_ERROR] Syntax error at or near 'EXISTS'(line 1, pos 41)

== SQL ==

ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name type

-----------------------------------------^^^>

1 REPLY 1

UmaMahesh1
Honored Contributor III

Hi @Christine Pedersen​ 

I guess IF NOT EXISTS or IF EXISTS can be used in conjunction with DROP or PARTITIONS according to the documentation.

If you want to do this the same checking way, you can do using a try catch block in pyspark or as per your language requirement.

import pyspark.sql.utils
try:
  spark.sql("""ALTER TABLE table-name ADD COLUMNS  col-name data-type""")
except pyspark.sql.utils.AnalysisException:
  print("Column already exists")

Hope this helps...

Cheers.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.