cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

tbl name as paramater marker

smpa01
Contributor

I am getting an error here, when I do this

//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;

 

doc1 doc2 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions

lingareddy_Alva
Honored Contributor III

@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

View solution in original post

1 REPLY 1

lingareddy_Alva
Honored Contributor III

@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