<?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: Query table based on table_name from information_schema in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/query-table-based-on-table-name-from-information-schema/m-p/67972#M33499</link>
    <description>&lt;P&gt;The simpliest way would be propably using spark.sql&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%py
tbl_name = 'table_v1'
df = spark.sql(f'select * from {tbl_name}')
display(df)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;From there, You can simply create temporary view:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%py
df.createOrReplaceTempView('table_act')&lt;/LI-CODE&gt;&lt;P&gt;and query it using SQL statements:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%sql
select * from table_act order by 1 asc;&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 02 May 2024 20:31:45 GMT</pubDate>
    <dc:creator>radothede</dc:creator>
    <dc:date>2024-05-02T20:31:45Z</dc:date>
    <item>
      <title>Query table based on table_name from information_schema</title>
      <link>https://community.databricks.com/t5/data-engineering/query-table-based-on-table-name-from-information-schema/m-p/67968#M33497</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have one table that changes the name every 60 days. The name simple increases the number version, for example:&lt;BR /&gt;* Firtst 60 days: table_name_v1. After 60 days:&amp;nbsp;table_name_v2 and so on.&lt;/P&gt;&lt;P&gt;What i want is to query the table wich name returned in the query of information_schema. This is important because when the name change, I won't need to adapt the query name.&amp;nbsp;&lt;BR /&gt;Here is a example of what I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CREATE&lt;/SPAN&gt; &lt;SPAN&gt;OR&lt;/SPAN&gt; &lt;SPAN&gt;REPLACE&lt;/SPAN&gt; &lt;SPAN&gt;TEMP&lt;/SPAN&gt; &lt;SPAN&gt;VIEW&lt;/SPAN&gt;&lt;SPAN&gt; db_copart &lt;/SPAN&gt;&lt;SPAN&gt;AS&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;SELECT&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; table_name&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;FROM&lt;/SPAN&gt;&lt;SPAN&gt; information_schema&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;tables&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;WHERE&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; table_schema = '&lt;/SPAN&gt;&lt;SPAN&gt;table_sandbox'&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;and&lt;/SPAN&gt;&lt;SPAN&gt; table_name &lt;/SPAN&gt;&lt;SPAN&gt;ILIKE&lt;/SPAN&gt; &lt;SPAN&gt;'%db_pi_copart_v%'&lt;/SPAN&gt;&lt;SPAN&gt;; -- this will return the latest table version&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;SELECT&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;*&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;FROM&lt;/SPAN&gt;&lt;SPAN&gt; table_sandbox.(select table_name from&amp;nbsp;db_copart)&lt;BR /&gt;&lt;BR /&gt;Thank you all for any help.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 19:34:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/query-table-based-on-table-name-from-information-schema/m-p/67968#M33497</guid>
      <dc:creator>nilton</dc:creator>
      <dc:date>2024-05-02T19:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Query table based on table_name from information_schema</title>
      <link>https://community.databricks.com/t5/data-engineering/query-table-based-on-table-name-from-information-schema/m-p/67972#M33499</link>
      <description>&lt;P&gt;The simpliest way would be propably using spark.sql&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%py
tbl_name = 'table_v1'
df = spark.sql(f'select * from {tbl_name}')
display(df)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;From there, You can simply create temporary view:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%py
df.createOrReplaceTempView('table_act')&lt;/LI-CODE&gt;&lt;P&gt;and query it using SQL statements:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%sql
select * from table_act order by 1 asc;&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 02 May 2024 20:31:45 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/query-table-based-on-table-name-from-information-schema/m-p/67972#M33499</guid>
      <dc:creator>radothede</dc:creator>
      <dc:date>2024-05-02T20:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: Query table based on table_name from information_schema</title>
      <link>https://community.databricks.com/t5/data-engineering/query-table-based-on-table-name-from-information-schema/m-p/67973#M33500</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/104480"&gt;@radothede&lt;/a&gt;&amp;nbsp;, but in Databricks SQL I can only use SQL, as far as I know&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 20:35:01 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/query-table-based-on-table-name-from-information-schema/m-p/67973#M33500</guid>
      <dc:creator>nilton</dc:creator>
      <dc:date>2024-05-02T20:35:01Z</dc:date>
    </item>
  </channel>
</rss>

