cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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 II

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.

Uma Mahesh D

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group