@smpa01
In SQL EXECUTE IMMEDIATE, you can only parameterize values, not identifiers like table names, column names, or database names.
That is, placeholders (?) can only replace constant values, not object names (tables, schemas, columns, etc.).
SELECT col1 FROM (?) LIMIT (?) this is invalid.
because (?) is trying to replace a table name, which is not allowed.
Databricks SQL and ANSI SQL standards say:
"Placeholders (?) can only bind to literal values, not database objects (like table names or column names)."
Correct Way to Handle Dynamic Table Names
If you want to dynamically choose the table name, you must build the full SQL string manually, before executing it.
Hereโs how you can fix it:
-- Define the table name and limit separately
DECLARE tblName STRING;
DECLARE limitVal INT;
SET tblName = 'catalog.schema.tbl';
SET limitVal = 500;
-- Build the query string manually
DECLARE sqlStr STRING;
SET sqlStr = CONCAT('SELECT col1 FROM ', tblName, ' LIMIT ', CAST(limitVal AS STRING));
-- Now execute
EXECUTE IMMEDIATE sqlStr;
Note: No placeholder for table names. Only manual string building allowed for identifiers.
LR