Details: SQL Macros

This page also provides more detailed information explaining what SQL Macros are useful for and supplying various examples to use.

You can create SQL macros (SQM) to factor out common SQL expressions and statements into reusable, parameterized constructs that can be used in other SQL statements. SQL macros can either be scalar expressions, typically used in SELECT lists, WHERE, and HAVING clauses, to encapsulate calculations and business logic, or can be table expressions, typically used in a FROM clause, to act as a sort of parameterized views. SQL macros increase developer productivity, simplifies collaborative development, and improves code quality.

The example in the slide shows an SQL macro written as a scalar expression, used in the SELECT list of the query.

The example in the slide shows an SQL macro written as a scalar expression, used in the SELECT list and the WHERE clause of the query.

The example in the slide shows an SQL macro written as a table expression, then used in the FROM list of the query.