Adding a Column to Spark Table via SQL ALTER TABLE command

11,286

In Spark SQL the syntax is as mentioned by Soumyadip Ghosh in the comments

ALTER TABLE table_identifier ADD COLUMNS ( col_spec [ , ... ] )

Works for me.

Share:
11,286
Soumyadip Ghosh
Author by

Soumyadip Ghosh

Updated on June 05, 2022

Comments

  • Soumyadip Ghosh
    Soumyadip Ghosh almost 2 years

    Can I add a new column to an existing spark table using the ALTER TABLE command ?

    var query = "ALTER TABLE " + "global_temp." + tableName(0) + " ADD COLUMN " + newColumnName + " " + newColumnDatatype
    var drt = spark.sql(query)
    

    The above code raises the following error.

     no viable alternative at input 'ALTER TABLE global_temp.people_ty ADD COLUMN' new_age integer
    

    EDIT

    The correct syntax is as follows

    ALTER TABLE tablename ADD COLUMNS (newColumn newDataType)
    

    But, it also throws the following error.

    ALTER ADD COLUMNS does not support views.
    You must drop and re-create the views for adding the new columns. Views: `global_temp`.`people_ty`