<?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: Execute Immediate not working to fetch table name based on year in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129782#M10611</link>
    <description>&lt;P&gt;Hi Martinson,&lt;/P&gt;&lt;P&gt;Thank you for the response. I tried using VAR and it still throws syntax error at || . so I tried $ it still has same error:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;[PARSE_SYNTAX_ERROR] Syntax error at or near '||'. SQLSTATE: 42601 line 3, pos 95 &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Aug 2025 10:04:47 GMT</pubDate>
    <dc:creator>MR_DHC</dc:creator>
    <dc:date>2025-08-26T10:04:47Z</dc:date>
    <item>
      <title>Execute Immediate not working to fetch table name based on year</title>
      <link>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129778#M10609</link>
      <description>&lt;P&gt;&amp;nbsp;I am trying to pass the year as argument so it can be used in the table name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ex: there are tables like Claims_total_2021 , Claims_total_2022 and so on till 2025. Now I want to pass the year in parameter , say 2024 and it must fetch the table Claims_total_2024 from database.&lt;/P&gt;&lt;P&gt;this is the code I am trying :&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN&gt; claim_year &lt;/SPAN&gt;&lt;SPAN&gt;STRING&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;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;SPAN&gt;IMMEDIATE&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;'SELECT 1&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;FROM Claims_total_'&lt;/SPAN&gt; &lt;SPAN&gt;||&lt;/SPAN&gt;&lt;SPAN&gt; claim_year &lt;/SPAN&gt;&lt;SPAN&gt;||&lt;/SPAN&gt; &lt;SPAN&gt;'&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;LIMIT 10'&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;so this must query from Claims_total_2024 in the query.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;This is throwing the following error :&amp;nbsp;[CONFIG_NOT_AVAILABLE] Configuration claim_year is not available. SQLSTATE: 42K0I&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Any suggestion would be appreciated.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 26 Aug 2025 09:41:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129778#M10609</guid>
      <dc:creator>MR_DHC</dc:creator>
      <dc:date>2025-08-26T09:41:53Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Immediate not working to fetch table name based on year</title>
      <link>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129781#M10610</link>
      <description>&lt;P&gt;Without VAR, the SET command attempts to set a Spark session configuration instead.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DECLARE claim_year STRING;

SET VAR claim_year = (
SELECT CAST(CLAIM_YEAR - 1 AS STRING)
FROM dbengineering_prod.claimscommercial.clms_commercial_ref_years R
WHERE R.ANALYSIS_TYPE_ID = 1
AND R.YEAR_RANK = 1
);

EXECUTE IMMEDIATE
'SELECT 1
FROM `dbengineering_prod.claimscommercial.Claims_total_' || claim_year || '`
LIMIT 10';&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SET Variable info-&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-aux-set-variable" target="_blank" rel="noopener"&gt;https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-aux-set-variable&lt;/A&gt;&lt;/P&gt;&lt;P&gt;You need to disambiguate this due to the shared syntax between Databricks SQL and Spark, in Spark SET is commonly used in configs.&lt;/P&gt;&lt;P&gt;Info on Spark Configuration&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://spark.apache.org/docs/latest/configuration.html" target="_blank" rel="noopener"&gt;https://spark.apache.org/docs/latest/configuration.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Aug 2025 10:05:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129781#M10610</guid>
      <dc:creator>martinson</dc:creator>
      <dc:date>2025-08-26T10:05:56Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Immediate not working to fetch table name based on year</title>
      <link>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129782#M10611</link>
      <description>&lt;P&gt;Hi Martinson,&lt;/P&gt;&lt;P&gt;Thank you for the response. I tried using VAR and it still throws syntax error at || . so I tried $ it still has same error:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;[PARSE_SYNTAX_ERROR] Syntax error at or near '||'. SQLSTATE: 42601 line 3, pos 95 &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Aug 2025 10:04:47 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129782#M10611</guid>
      <dc:creator>MR_DHC</dc:creator>
      <dc:date>2025-08-26T10:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Immediate not working to fetch table name based on year</title>
      <link>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129792#M10612</link>
      <description>&lt;P&gt;EXECUTE IMMEDIATE requires the parameter to either be a string or a variable. You are trying to pass an expression through, causing the error. I believe that if you set up a SET VAR with the expression and then pass that through, that should solve your issue. Here is an example of what that could look like.&lt;/P&gt;&lt;P&gt;Let me know if that works or the error it returns&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DECLARE claim_year STRING;

SET VAR claim_year = (
  SELECT CAST(CLAIM_YEAR - 1 AS STRING)
  FROM dbengineering_prod.claimscommercial.clms_commercial_ref_years R
  WHERE R.ANALYSIS_TYPE_ID = 1
    AND R.YEAR_RANK = 1
);

DECLARE sql_str STRING;

SET VAR sql_str = 'SELECT 1
   FROM `dbengineering_prod.claimscommercial.Claims_total_' || claim_year || '`
   LIMIT 10';

EXECUTE IMMEDIATE sql_str;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Aug 2025 10:18:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129792#M10612</guid>
      <dc:creator>martinson</dc:creator>
      <dc:date>2025-08-26T10:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Immediate not working to fetch table name based on year</title>
      <link>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129800#M10615</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I believe the solution shared by Martison would fix this issue.&lt;/P&gt;
&lt;P&gt;In Databricks SQL, when using EXECUTE IMMEDIATE, the SQL string must be a single variable or single string literal, not an inline expression using string concatenation (&lt;CODE class="qt3gz9f"&gt;'...' || claim_year || '...'&lt;/CODE&gt;).&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-aux-execute-immediate" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-aux-execute-immediate&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;DECLARE claim_year STRING;&lt;BR /&gt;SET VAR claim_year = (&lt;BR /&gt;SELECT CAST(CLAIM_YEAR - 1 AS STRING)&lt;BR /&gt;FROM dbengineering_prod.claimscommercial.clms_commercial_ref_years R&lt;BR /&gt;WHERE R.ANALYSIS_TYPE_ID = 1 AND R.YEAR_RANK = 1&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;DECLARE sqlstr STRING;&lt;BR /&gt;SET VAR sqlstr = 'SELECT 1 FROM Claims_total_' || claim_year || ' LIMIT 10';&lt;BR /&gt;&lt;BR /&gt;EXECUTE IMMEDIATE sqlstr;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Aug 2025 10:36:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129800#M10615</guid>
      <dc:creator>Sidhant07</dc:creator>
      <dc:date>2025-08-26T10:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Immediate not working to fetch table name based on year</title>
      <link>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129806#M10617</link>
      <description>&lt;P&gt;Hi Again!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry to trouble , this is still causing the error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;[PARSE_SYNTAX_ERROR] Syntax error at or near '$'. SQLSTATE: 42601 == SQL (line 1, position 1) == EXECUTE IMMEDIATE sql_str&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Aug 2025 10:50:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129806#M10617</guid>
      <dc:creator>MR_DHC</dc:creator>
      <dc:date>2025-08-26T10:50:00Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Immediate not working to fetch table name based on year</title>
      <link>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129807#M10618</link>
      <description>&lt;P&gt;Hi MR_DHC! Can you try using '||' instead of '$'?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Aug 2025 10:54:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/execute-immediate-not-working-to-fetch-table-name-based-on-year/m-p/129807#M10618</guid>
      <dc:creator>martinson</dc:creator>
      <dc:date>2025-08-26T10:54:55Z</dc:date>
    </item>
  </channel>
</rss>

