<?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 How to set a variable and use it in a SQL query in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/22301#M15258</link>
    <description>&lt;P&gt;I want to define a variable and use it in a query, like below: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%sql
&amp;nbsp;
SET database_name = "marketing";
SHOW TABLES in '${database_name}';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I get the following error:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;ParseException: 
[PARSE_SYNTAX_ERROR] Syntax error at or near ''''(line 1, pos 15)
&amp;nbsp;
== SQL ==
SHOW TABLES in ''&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am aware that I can use "spark.sql("query")" and python f-string. But, I would like to know how to use SQL for this purpose. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks. &lt;/P&gt;</description>
    <pubDate>Tue, 15 Nov 2022 11:07:22 GMT</pubDate>
    <dc:creator>Mado</dc:creator>
    <dc:date>2022-11-15T11:07:22Z</dc:date>
    <item>
      <title>How to set a variable and use it in a SQL query</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/22301#M15258</link>
      <description>&lt;P&gt;I want to define a variable and use it in a query, like below: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%sql
&amp;nbsp;
SET database_name = "marketing";
SHOW TABLES in '${database_name}';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I get the following error:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;ParseException: 
[PARSE_SYNTAX_ERROR] Syntax error at or near ''''(line 1, pos 15)
&amp;nbsp;
== SQL ==
SHOW TABLES in ''&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am aware that I can use "spark.sql("query")" and python f-string. But, I would like to know how to use SQL for this purpose. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks. &lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2022 11:07:22 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/22301#M15258</guid>
      <dc:creator>Mado</dc:creator>
      <dc:date>2022-11-15T11:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable and use it in a SQL query</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/22302#M15259</link>
      <description>&lt;P&gt;Hi @Mohammad Saber​&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SET is used for Setting Spark parameters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think the closest alternative to what you are trying to achive is to use widgets&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/notebooks/widgets.html#language-sql" alt="https://docs.databricks.com/notebooks/widgets.html#language-sql" target="_blank"&gt;https://docs.databricks.com/notebooks/widgets.html#language-sql&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CREATE WIDGET TEXT database DEFAULT "customers_dev"&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SHOW TABLES IN "${database}"&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2022 19:37:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/22302#M15259</guid>
      <dc:creator>Pat</dc:creator>
      <dc:date>2022-11-15T19:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable and use it in a SQL query</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/46662#M28083</link>
      <description>&lt;P&gt;Another option is demonstrated by this example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%sql
 
SET database_name.var = marketing;
SHOW TABLES in ${database_name.var};


SET database_name.dummy= marketing;
SHOW TABLES in ${database_name.dummy};&lt;/LI-CODE&gt;&lt;UL&gt;&lt;LI&gt;do not use quotes&lt;/LI&gt;&lt;LI&gt;use format that is variableName.something and it will work in %sql.&amp;nbsp; I don't know why.&amp;nbsp; I attached an example from my environment.&amp;nbsp; I redacted names and data, but you can see that it works with .var and .dummy.&amp;nbsp; You'll have to trust that I didn't use quotes&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;BR /&gt;FYI: Widget Documentation explains in the blue Note that using the widget method will not work when you Run All or run from another notebook:&amp;nbsp;&lt;A href="https://docs.databricks.com/en/notebooks/widgets.html" target="_blank"&gt;Databricks widgets | Databricks on AWS&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 20:43:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/46662#M28083</guid>
      <dc:creator>CJS</dc:creator>
      <dc:date>2023-09-28T20:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable and use it in a SQL query</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/46671#M28084</link>
      <description>&lt;P&gt;lol that attachment worked SO poorly.&amp;nbsp; Here it is:&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 20:52:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/46671#M28084</guid>
      <dc:creator>CJS</dc:creator>
      <dc:date>2023-09-28T20:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable and use it in a SQL query</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/105789#M42261</link>
      <description>&lt;P&gt;Updated usage as of DBR 15.2+&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://docs.databricks.com/en/notebooks/widgets.html#use-widget-values-in-spark-sql-and-sql-warehouse" target="_blank"&gt;https://docs.databricks.com/en/notebooks/widgets.html#use-widget-values-in-spark-sql-and-sql-warehouse&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 19:04:26 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/105789#M42261</guid>
      <dc:creator>olufemi_anthony</dc:creator>
      <dc:date>2025-01-15T19:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable and use it in a SQL query</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/111090#M43787</link>
      <description>&lt;P&gt;CJS had the best answer by virtue of it being code-based rather than widget-based.&amp;nbsp; In a notebook where the value of the variable must continually be reset, widgets are suboptimal.&amp;nbsp; That said, the easiest way is to wrap the code in python:&lt;/P&gt;&lt;P&gt;%py&lt;/P&gt;&lt;P&gt;var1 = some_value ##string, int, whatever&lt;/P&gt;&lt;P&gt;var2 = some_other_value&amp;nbsp;&lt;/P&gt;&lt;P&gt;qry = f"select * from {var1} where some_field = {var2}" ##use python's format feature to place variables cleanly&lt;/P&gt;&lt;P&gt;display(spark.sql(qry))&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2025 01:01:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-set-a-variable-and-use-it-in-a-sql-query/m-p/111090#M43787</guid>
      <dc:creator>TomRenish</dc:creator>
      <dc:date>2025-02-25T01:01:53Z</dc:date>
    </item>
  </channel>
</rss>

