The error you're encountering is because you're trying to execute a SQL query on a saved query name, which is not a table or view in your database. The spark.sql()
function is used to run SQL queries directly on tables or views in your Spark session.
If you want to execute a saved query, you need to fetch the SQL text of the saved query first. However, Databricks does not provide a built-in function to fetch the SQL text of a saved query directly in a notebook.
As a workaround, you can manually copy the SQL text of your saved query and use it in your notebook. Here's an example:
sql_text = """
SELECT *
FROM your_table
WHERE your_condition
"""
results = spark.sql(sql_text)
display(results)
In this example, replace sql_text
with the actual SQL text of your saved query. This way, you're running the SQL query directly on your data, not trying to run it on the saved query name.
If you have a large number of saved queries and manually copying the SQL text is not feasible, you might need to consider using Databricks' REST API or CLI to automate the process of fetching the SQL text of your saved queries.