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`
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With