Links

Parameterize your SQL query

There are times you want to dynamically update your SQL queries based on results of previous calculations or other data in your notebook. Workspace supports this through SQL parameterization, which allows you to insert variables into your SQL queries. SQL parameterization is supported in both Python and R workspaces.
Example of parameterizing SQL queries

Using Python

Workspace uses Jinja to process your SQL queries and thus supports all Jinja syntax.
To insert a simple variable into your query, wrap it in double curly braces ({{ }}):
1
SELECT *
2
FROM dvdrentals.category C
3
WHERE C.category_id = {{ id }}
To insert an array in a WHERE ... IN statement, you can use theinclause filter:
1
SELECT *
2
FROM dvdrentals.category C
3
WHERE C.category_id IN {{ ids | inclause }}
We use prepared statements to ensure you cannot accidentally inject malicious code in your SQL query. If you want to escape this safety, use the sqlsafe filter to mark your parameter as safe:
1
SELECT *
2
FROM dvdrentals.category
3
WHERE {{column | sqlsafe}} = 10

Using R

Workspace uses jinjar to process your SQL queries and thus supports most Jinja syntax in R.
To insert a simple variable into your query, wrap it in double curly braces ({{ }}):
1
SELECT *
2
FROM dvdrentals.category C
3
WHERE C.category_id = {{ id }}
To insert an array in a WHERE ... IN statement, Workspace provides the join() function:
SELECT *
FROM dvdrentals.category C
WHERE C.category_id IN ( {{ join(ids, ",") }} )
Alternatively, you can choose to preprocess in R yourself and inject a string, like so:
ids_for_sql <- paste(ids, collapse=",")
1
SELECT *
2
FROM dvdrentals.category C
3
WHERE C.category_id IN ( {{ ids_for_sql }} )
Unlike for Python, Workspace can't protect against SQL malicious code injection in interpolated SQL queries, so parametrize your queries with care.