Using advanced syntax for SQL queries

SQL Query Interpolation Syntax

This page provides an overview of SQL interpolation techniques, focusing on how to dynamically inject parameterized values into SQL queries. It explains the differences between the <%= %> and {{ }} syntaxes for injecting frontend and backend variables, and offers practical examples for various data types and scenarios

Variable Injection Sources

  • Frontend variables (set in app configuration or by requesters/filters):

    • Use <%= %> syntax

  • Backend variables (from instance config or user attributes):

    • Use {{ }} syntax

String or Integer Values from User/Instance Context

For string attributes:

WHERE INDUSTRY = '\'{{ string_attribute }}\''

For numeric attributes:

WHERE INDUSTRY = '{{ numeric_attribute }}'

Checkbox Requester (Array) of Strings

Use "IN" instead of "=" and join syntax:

WHERE CATEGORY IN ('<%= requestersManager.requester_check.join("','") %>')

List of String Values from User Attributes/Instance Context

WHERE beer_kind IN ('{{ "\',\'".join(user.attributes.beer_kind) }}')

Array or List of Integers

For checkbox filter:

WHERE CATEGORY IN ('<%= requestersManager.requester_check.join(",") %>')

For user attributes:

WHERE beer_kind IN ('{{ ",".join(user.attributes.beer_kind) }}')

String Template Filtering

Based on user/instance context:

WHERE beer_kind LIKE '%{{ user.attributes.beer_kind }}%'

Based on requester value:

WHERE beer_kind LIKE '%<%= requestersManager.beerkind %>%'

List of String Templates from Checkbox Values

WHERE beer_kind LIKE '%<%=requestersManager.beerkind.length > 1 ?requestersManager.beerkind.join("%' OR beer_kind LIKE '%"):requestersManager.beerkind[0] %>%'

List of String Templates from User/Instance Context

WHERE beer_kind LIKE '%{{ ("%\' OR beer_kind LIKE \'%").join(user.attributes.beer_kind) if user.attributes.beer_kind|length > 0 else user.attributes.beer_kind[0] }}%'

This structure organizes the information into clear sections with appropriate headers, code blocks for SQL snippets, and consistent formatting.

Last updated