<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ADD COLUMN IF NOT EXISTS does not recognize &amp;quot;IF NOT EXIST&amp;quot;. How do I add a column to an existing delta table with SQL if the column does not already exist? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/add-column-if-not-exists-does-not-recognize-quot-if-not-exist/m-p/18597#M12370</link>
    <description>&lt;P&gt;Hi @Christine Pedersen​&amp;nbsp;&lt;/P&gt;&lt;P&gt;I guess IF NOT EXISTS or IF EXISTS can be used in conjunction with DROP or PARTITIONS according to the documentation. &lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hope this helps...&lt;/P&gt;&lt;P&gt;Cheers.&lt;/P&gt;</description>
    <pubDate>Mon, 05 Dec 2022 17:57:26 GMT</pubDate>
    <dc:creator>UmaMahesh1</dc:creator>
    <dc:date>2022-12-05T17:57:26Z</dc:date>
    <item>
      <title>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?</title>
      <link>https://community.databricks.com/t5/data-engineering/add-column-if-not-exists-does-not-recognize-quot-if-not-exist/m-p/18596#M12369</link>
      <description>&lt;P&gt;How do I add a column to an existing delta table with SQL if the column does not already exist?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am using the following code: &lt;/P&gt;&lt;P&gt;&amp;lt;%sql&lt;/P&gt;&lt;P&gt;ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name type;&amp;nbsp;&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but it prints the error: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;[PARSE_SYNTAX_ERROR] Syntax error at or near 'EXISTS'(line 1, pos 41)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;== SQL ==&lt;/P&gt;&lt;P&gt;ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name type&lt;/P&gt;&lt;P&gt;-----------------------------------------^^^&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 13:55:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/add-column-if-not-exists-does-not-recognize-quot-if-not-exist/m-p/18596#M12369</guid>
      <dc:creator>Christine</dc:creator>
      <dc:date>2022-12-05T13:55:55Z</dc:date>
    </item>
    <item>
      <title>Re: 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?</title>
      <link>https://community.databricks.com/t5/data-engineering/add-column-if-not-exists-does-not-recognize-quot-if-not-exist/m-p/18597#M12370</link>
      <description>&lt;P&gt;Hi @Christine Pedersen​&amp;nbsp;&lt;/P&gt;&lt;P&gt;I guess IF NOT EXISTS or IF EXISTS can be used in conjunction with DROP or PARTITIONS according to the documentation. &lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hope this helps...&lt;/P&gt;&lt;P&gt;Cheers.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 17:57:26 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/add-column-if-not-exists-does-not-recognize-quot-if-not-exist/m-p/18597#M12370</guid>
      <dc:creator>UmaMahesh1</dc:creator>
      <dc:date>2022-12-05T17:57:26Z</dc:date>
    </item>
  </channel>
</rss>

