DROP Multiple Tables in Database using Databricks
Asked Answered
C

2

5

I am trying to drop multiple tables in databrick scala using the following command

select 'DROP TABLE ' + tableName from ABC where tableName LIKE 'in%'

Error results saying

Error in SQL statement: AnalysisException: Table or view not found: ABC

However the ABC database is present

Thank you

Careen answered 22/8, 2022 at 2:51 Comment(0)
S
6

No, it doesn't work this way... You need to use SHOW TABLES IN ... LIKE ... in combination with the explicit deletes. Something like this (in Python):

db_name = "ABC"
pattern = "in*"
tables = spark.sql(f"SHOW TABLES IN {db_name} LIKE '{pattern}'")
for row in tables.collect():
  spark.sql(f"DROP TABLE {db_name}.{row[1]}")
Stalemate answered 22/8, 2022 at 12:9 Comment(1)
@hsop at time of answer, the % characters was used...Stalemate
C
1

I found the following way in scala

val dbName="ABC"
val df = spark.sql(s"""show tables from """+ dbName)
df.createOrReplaceTempView("temp_tables")
val temp_tables = spark.sql("""select tableName from temp_tables where tableName like 'in%' """)

temp_tables.collect().foreach(row => println("DROP TABLE " + dbName + "."+ row.toString().replace("[", "").replace("]", "")))

Careen answered 23/8, 2022 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.