Member-only story

SQL Parameterization in Databricks SQL

Hubert Dudek
3 min readMar 21, 2025

--

Databricks SQL Warehouse supports robust parameterization, which you must know before starting with SQL scripting, which was introduced in runtime 16.3.
Databricks Runtime 16.1 enhanced parameterization capabilities by allowing IDENTIFIER usage in the USE CATALOG statement.
This article uses a structured approach to explore variables, parameter markers, and EXECUTE IMMEDIATE.

If you are not yet a member of Medium, you can access for free via a friend’s link

Variables in Databricks SQL

Variables store temporary values that persist within a session. They can be directly assigned or dynamically set with a query.

DECLARE OR REPLACE VARIABLE sal_range INT;
SET VAR sal_range = 20000;

SELECT * FROM employees
WHERE sal > sal_range;

Parameter Markers (Widgets in SQL Editor)

Databricks SQL Editor and notebooks support widgets (UI parameters) for dynamic user input. You can also pass them through jobs. These are referenced with :param_name syntax inside queries.

SELECT * FROM employees
WHERE sal > :sal_range;

Using IDENTIFIER for Dynamic Objects

IDENTIFIER() allows dynamic substitution of object names (catalog, schema, table) to prevent SQL injection.

DECLARE OR REPLACE VARIABLE table_name STRING = 'employees';

SELECT * FROM IDENTIFIER(table_name);

Dynamic SQL with EXECUTE IMMEDIATE

EXECUTE IMMEDIATE executes a dynamically constructed SQL string at runtime.

DECLARE OR REPLACE sqlStr STRING = 'SELECT * FROM employees WHERE sal > ?';

EXECUTE IMMEDIATE sqlStr USING 2000;

Parameterizing USE CATALOG (Databricks 16.1 Update)

As of Runtime 16.1, USE CATALOG supports IDENTIFIER, enabling flexible catalog selection.

DECLARE OR REPLACE VARIABLE catalog_name STRING = 'workspace';

USE CATALOG IDENTIFIER(catalog_name);

Combining Variables and parameters with EXECUTE IMMEDIATE

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

--

--

No responses yet

Write a response