Skip to main content
SDF supports simple variables and parameterization in SQL queries. This is useful for dynamic queries and for reusing values in multiple queries. These differ from variables in Jinja in the following ways:
  1. They are only available for use in SQL, not in YML.
  2. They are typed (bool, int, float, string, and null)
  3. They are used with the @ syntax, instead of \{\{ \}\}
  4. They can be easily set per run with the --vars flag on CLI commands like sdf compile and sdf run
Let’s go through a simple example where we use a variable in a SQL query.

Example

In this example, we will create a variable @my_variable and use it in a SQL query.
In order to enable variable substitution, we’ll need to set the preprocessor option to sql-vars in the workspace.sdf.yml like so:
workspace.sdf.yml
Next, to set the value of @my_variable, let’s add a vars yml block to the workspace like so:
workspace.sdf.yml
You can use the --vars flag on the CLI. For example:
or
This will replace @my_variable with my_value in the SQL query. The resulting query will look like this:
SDF vars also respect types, so if you set a variable to a number, it will be treated as a number in the SQL query. For example:
This will replace @my_variable with 123 in the SQL query. The resulting query will look like this:
SDF vars also support boolean, float, and null types along with strings and integers.

Vars in Jinja

SQL variables can also be referenced and utilized in Jinja. For example, if we modify the query above to use jinja substitution syntax, it would look like this:
You can then set the variable in the vars block in the workspace.sdf.yml file like so:
workspace.sdf.yml
If you want to use both Jinja and SQL vars in preprocessing, you’ll need to set preprocessor to all in the workspace.sdf.yml as seen above.

Vars in YML

You can also set variables in a vars.yml file. This is useful when you have a lot of variables to set. Here is an example of a vars.yml file:
vars.yml
You can then use this file with the --vars flag like so:
where vars.yml is the path to the file.