<?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: tbl name as paramater marker in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/tbl-name-as-paramater-marker/m-p/116835#M45383</link>
    <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/8654"&gt;@smpa01&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In SQL EXECUTE IMMEDIATE, you can only parameterize values, not identifiers like table names, column names, or database names.&lt;BR /&gt;That is, placeholders (?) can only replace constant values, not object names (tables, schemas, columns, etc.).&lt;/P&gt;&lt;P&gt;SELECT col1 FROM (?) LIMIT (?) this is invalid.&lt;BR /&gt;because (?) is trying to replace a table name, which is not allowed.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Databricks SQL and ANSI SQL standards say:&lt;/STRONG&gt;&lt;BR /&gt;"Placeholders (?) can only bind to literal values, not database objects (like table names or column names)."&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Correct Way to Handle Dynamic Table Names&lt;/STRONG&gt;&lt;BR /&gt;If you want to dynamically choose the table name, you must build the full SQL string manually, before executing it.&lt;BR /&gt;Here’s how you can fix it:&lt;/P&gt;&lt;P&gt;-- Define the table name and limit separately&lt;BR /&gt;DECLARE tblName STRING;&lt;BR /&gt;DECLARE limitVal INT;&lt;/P&gt;&lt;P&gt;SET tblName = 'catalog.schema.tbl';&lt;BR /&gt;SET limitVal = 500;&lt;/P&gt;&lt;P&gt;-- Build the query string manually&lt;BR /&gt;DECLARE sqlStr STRING;&lt;BR /&gt;SET sqlStr = CONCAT('SELECT col1 FROM ', tblName, ' LIMIT ', CAST(limitVal AS STRING));&lt;/P&gt;&lt;P&gt;-- Now execute&lt;BR /&gt;EXECUTE IMMEDIATE sqlStr;&lt;/P&gt;&lt;P&gt;Note: No placeholder for table names. Only manual string building allowed for identifiers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Apr 2025 19:30:13 GMT</pubDate>
    <dc:creator>lingareddy_Alva</dc:creator>
    <dc:date>2025-04-28T19:30:13Z</dc:date>
    <item>
      <title>tbl name as paramater marker</title>
      <link>https://community.databricks.com/t5/data-engineering/tbl-name-as-paramater-marker/m-p/116831#M45381</link>
      <description>&lt;P&gt;I am getting an error here, when I do this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;//this works fine
declare sqlStr = 'select col1 from catalog.schema.tbl LIMIT (?)';
declare arg1 = 500;
EXECUTE IMMEDIATE sqlStr USING arg1;

//this does not
declare sqlStr = 'select col1 from (?) LIMIT (?)';
declare arg1 = 'catalog.schema.tbl'
declare arg2 = 500;
EXECUTE IMMEDIATE sqlStr USING arg1,arg2;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&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="_self"&gt;doc1&lt;/A&gt;&amp;nbsp;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-parameter-marker" target="_self"&gt;doc2&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Mon, 28 Apr 2025 18:31:18 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/tbl-name-as-paramater-marker/m-p/116831#M45381</guid>
      <dc:creator>smpa01</dc:creator>
      <dc:date>2025-04-28T18:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: tbl name as paramater marker</title>
      <link>https://community.databricks.com/t5/data-engineering/tbl-name-as-paramater-marker/m-p/116835#M45383</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/8654"&gt;@smpa01&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In SQL EXECUTE IMMEDIATE, you can only parameterize values, not identifiers like table names, column names, or database names.&lt;BR /&gt;That is, placeholders (?) can only replace constant values, not object names (tables, schemas, columns, etc.).&lt;/P&gt;&lt;P&gt;SELECT col1 FROM (?) LIMIT (?) this is invalid.&lt;BR /&gt;because (?) is trying to replace a table name, which is not allowed.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Databricks SQL and ANSI SQL standards say:&lt;/STRONG&gt;&lt;BR /&gt;"Placeholders (?) can only bind to literal values, not database objects (like table names or column names)."&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Correct Way to Handle Dynamic Table Names&lt;/STRONG&gt;&lt;BR /&gt;If you want to dynamically choose the table name, you must build the full SQL string manually, before executing it.&lt;BR /&gt;Here’s how you can fix it:&lt;/P&gt;&lt;P&gt;-- Define the table name and limit separately&lt;BR /&gt;DECLARE tblName STRING;&lt;BR /&gt;DECLARE limitVal INT;&lt;/P&gt;&lt;P&gt;SET tblName = 'catalog.schema.tbl';&lt;BR /&gt;SET limitVal = 500;&lt;/P&gt;&lt;P&gt;-- Build the query string manually&lt;BR /&gt;DECLARE sqlStr STRING;&lt;BR /&gt;SET sqlStr = CONCAT('SELECT col1 FROM ', tblName, ' LIMIT ', CAST(limitVal AS STRING));&lt;/P&gt;&lt;P&gt;-- Now execute&lt;BR /&gt;EXECUTE IMMEDIATE sqlStr;&lt;/P&gt;&lt;P&gt;Note: No placeholder for table names. Only manual string building allowed for identifiers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Apr 2025 19:30:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/tbl-name-as-paramater-marker/m-p/116835#M45383</guid>
      <dc:creator>lingareddy_Alva</dc:creator>
      <dc:date>2025-04-28T19:30:13Z</dc:date>
    </item>
  </channel>
</rss>

