Soumyadip Ghosh
Soumyadip Ghosh

Reputation: 188

Adding a Column to Spark Table via SQL ALTER TABLE command

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`

Upvotes: 7

Views: 24247

Answers (1)

Reed Richards
Reed Richards

Reputation: 4378

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.

Upvotes: 5

Related Questions