<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to run a saved query from a Notebook (PySpark) in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/122890#M46898</link>
    <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/82106"&gt;@uday_satapathy&lt;/a&gt;&amp;nbsp;Hi Uday. Do you know if this method works for many users? In case I need to share the script so a teammate may use it.&lt;/P&gt;</description>
    <pubDate>Wed, 25 Jun 2025 23:58:40 GMT</pubDate>
    <dc:creator>aethorimn_cgr</dc:creator>
    <dc:date>2025-06-25T23:58:40Z</dc:date>
    <item>
      <title>How to run a saved query from a Notebook (PySpark)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/55933#M30469</link>
      <description>&lt;P&gt;Hi Team! Noob to Databricks, so apologies if I ask a dumb question.&lt;/P&gt;&lt;P&gt;I have created a relatively large series of queries that fetch and organize the data I want.&amp;nbsp; I'm ready to drive all of these from a Notebook (likely PySpark).&lt;/P&gt;&lt;P&gt;An example query is saved as &lt;FONT color="#FF9900"&gt;&lt;STRONG&gt;test1&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;under my user account (it shows up under the Queries heading in the left navbar).&amp;nbsp; I've also created a Notebook under that same account called &lt;FONT color="#FF9900"&gt;&lt;STRONG&gt;Investigations&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;(and both the notebook and query show up under Workspace).&lt;/P&gt;&lt;P&gt;My sample code is simple:&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;query_name &lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;"&lt;/SPAN&gt;&lt;SPAN&gt;test1&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;results &lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt; spark.sql(&lt;/SPAN&gt;&lt;SPAN&gt;f"&lt;/SPAN&gt;&lt;SPAN&gt;SELECT&lt;/SPAN&gt; &lt;SPAN&gt;*&lt;/SPAN&gt; &lt;SPAN&gt;FROM&lt;/SPAN&gt;&lt;SPAN&gt; {query_name}&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#&lt;/SPAN&gt;&lt;SPAN&gt; Show the results&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;display(results)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;But it produces the&amp;nbsp;TABLE_OR_VIEW_NOT_FOUND error:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;AnalysisException: &lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;A class="" href="https://docs.databricks.com/error-messages/error-classes.html#table_or_view_not_found" target="_blank" rel="noopener noreferrer"&gt;TABLE_OR_VIEW_NOT_FOUND&lt;/A&gt;&lt;SPAN&gt;]&lt;/SPAN&gt;&lt;SPAN&gt; The table or view `test1` cannot be found. Verify the spelling and correctness of the schema and catalog. If you did not qualify the name with a schema, verify the current_schema() output, or qualify the name with the correct schema and catalog.&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;I'm sure the issue is just that I'm not properly referencing the query.&amp;nbsp; Help!!&lt;/P&gt;</description>
      <pubDate>Fri, 29 Dec 2023 18:17:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/55933#M30469</guid>
      <dc:creator>pacman</dc:creator>
      <dc:date>2023-12-29T18:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a saved query from a Notebook (PySpark)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/55940#M30472</link>
      <description>&lt;P&gt;Databricks is just pasting string value "test1" inside your code , so it is equivalent of running SQL command.&lt;/P&gt;&lt;P&gt;SELECT * FROM test1&lt;/P&gt;&lt;P&gt;So it is expecting to read data from existing table test1, but it can't find it&lt;/P&gt;&lt;P&gt;All tables can be referenced by providing catalog name , schema name and table name like: my_catalog.my_schema.test1&lt;/P&gt;&lt;P&gt;Or&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can point your current session to use certain calatlog and schema , like : USE my_catalog.my_schema&lt;/P&gt;&lt;P&gt;Then it will always try to search a table in this schema and you can refere to table like you did.&lt;/P&gt;&lt;P&gt;I am not sure if this is what you are looking for but if it won't help,&amp;nbsp; please attach screeahot from your UI&lt;/P&gt;</description>
      <pubDate>Fri, 29 Dec 2023 23:13:33 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/55940#M30472</guid>
      <dc:creator>Wojciech_BUK</dc:creator>
      <dc:date>2023-12-29T23:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a saved query from a Notebook (PySpark)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/64438#M32571</link>
      <description>&lt;P class="_1t7bu9h1 paragraph"&gt;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 &lt;CODE&gt;spark.sql()&lt;/CODE&gt; function is used to run SQL queries directly on tables or views in your Spark session.&lt;/P&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;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.&lt;/P&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;As a workaround, you can manually copy the SQL text of your saved query and use it in your notebook. Here's an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="_1t7bu9hb markdown-code-python hljs language-python" data-highlighted="yes"&gt;&lt;SPAN class="hljs-comment"&gt;# Replace this with the SQL text of your saved query&lt;/SPAN&gt;
sql_text = &lt;SPAN class="hljs-string"&gt;"""
SELECT *
FROM your_table
WHERE your_condition
"""&lt;/SPAN&gt;

results = spark.sql(sql_text)

&lt;SPAN class="hljs-comment"&gt;# Show the results&lt;/SPAN&gt;
display(results)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV class=" iwpqfy0"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;In this example, replace &lt;CODE&gt;sql_text&lt;/CODE&gt; 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.&lt;/P&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;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.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Mar 2024 14:14:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/64438#M32571</guid>
      <dc:creator>Walter_C</dc:creator>
      <dc:date>2024-03-23T14:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a saved query from a Notebook (PySpark)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/94635#M38939</link>
      <description>&lt;P&gt;You can do something like this using databricks-sdk:&lt;/P&gt;
&lt;P&gt;Here's the saved query&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Screenshot 2024-10-17 at 2.48.51 PM.png" style="width: 999px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/12043i7E47746DE3BB7907/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-10-17 at 2.48.51 PM.png" alt="Screenshot 2024-10-17 at 2.48.51 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pyspark code:&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;LI-CODE lang="python"&gt;import re
from pyspark.sql import DataFrame
from databricks.sdk import WorkspaceClient

def run_saved_query(query_id:str, **params) -&amp;gt; DataFrame:
  query_body, args = get_query_by_id(query_id)
  query = replace_double_curly(query_body)
  df = spark.sql(query, **params)
  return df

def get_query_by_id(query_id):
  w = WorkspaceClient()
  q = w.queries.get(query_id)
  query_body = q.query
  query_params = [p.name for p in q.options.parameters]
  return query_body, query_params

def replace_double_curly(text):
    # Adjust regex to match the outermost double curly braces without capturing nested ones
    def strip_outer_whitespace(match):
        inner_text = match.group(1)
        return f'{{{inner_text.strip()}}}'
    
    # Match only the outermost {{...}} braces
    result = re.sub(r'\{\{\s*([^{}]*?)\s*\}\}', strip_outer_whitespace, text)
    return result

params = {"year": 2016}
df = run_saved_query('d045b229-e5d0-4de0-bcbc-bec6cbba61d7', **params)
df.display()
&lt;/LI-CODE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Screenshot 2024-10-17 at 2.54.03 PM.png" style="width: 999px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/12044iCD2BD2B3BA1BFE69/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-10-17 at 2.54.03 PM.png" alt="Screenshot 2024-10-17 at 2.54.03 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Thu, 17 Oct 2024 18:54:47 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/94635#M38939</guid>
      <dc:creator>uday_satapathy</dc:creator>
      <dc:date>2024-10-17T18:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a saved query from a Notebook (PySpark)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/103268#M41388</link>
      <description>&lt;P&gt;With the new update to the Databricks SQL editor, how would one do this now? A query's id is no longer in the query URL&lt;/P&gt;</description>
      <pubDate>Thu, 26 Dec 2024 22:48:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/103268#M41388</guid>
      <dc:creator>cloudy_data</dc:creator>
      <dc:date>2024-12-26T22:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a saved query from a Notebook (PySpark)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/104159#M41672</link>
      <description>&lt;P&gt;How does it shows to you when opening a saved query in the new editor? I have checked on my side and it is still showing the id.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 22:20:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/104159#M41672</guid>
      <dc:creator>Walter_C</dc:creator>
      <dc:date>2025-01-03T22:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a saved query from a Notebook (PySpark)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/104160#M41673</link>
      <description>&lt;P&gt;You can also get the query ids by listing the queries through API call&amp;nbsp;&lt;A href="https://docs.databricks.com/api/workspace/queries/list" target="_blank"&gt;https://docs.databricks.com/api/workspace/queries/list&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 22:23:05 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/104160#M41673</guid>
      <dc:creator>Walter_C</dc:creator>
      <dc:date>2025-01-03T22:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a saved query from a Notebook (PySpark)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/122890#M46898</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/82106"&gt;@uday_satapathy&lt;/a&gt;&amp;nbsp;Hi Uday. Do you know if this method works for many users? In case I need to share the script so a teammate may use it.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jun 2025 23:58:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-run-a-saved-query-from-a-notebook-pyspark/m-p/122890#M46898</guid>
      <dc:creator>aethorimn_cgr</dc:creator>
      <dc:date>2025-06-25T23:58:40Z</dc:date>
    </item>
  </channel>
</rss>

