A Parameterized Statement is a SQL statement templated with arguments called parameters
They are runtime where
An analytics SQL Select statement with 1 placeholder (ie 1 parameter)
select d_week_seq1
,round(sun_sales1/sun_sales2,2)
,round(mon_sales1/mon_sales2,2)
where
d_year = ?
group by
d_week_seq1
An DML insert statement with 2 placeholders (ie 2 parameters)
INSERT INTO products (name, price) VALUES (?, ?);
An stored procedure statement with 2 placeholders (ie 2 parameters)
Example
? = call upper(?)
{ ? = call upper(?) }
SQL Parameters are a list of parameter that can be used to parametrized DML statements.
In a command comparison, they are the arguments of a SQL Statement
Parameters are also known as bind variables
You can add to a SQL request, the parameters attributes
Example of SQL Request manifest
kind: sql-request
spec:
data-uri: (script.sql@cd)@database
data-def:
parameters:
# the value (default: null)
- value: value1
# The direction: IN (default), OUT, INOUT
direction: IN
# the data type (mandatory when the value is null)
type: varchar
# the column name if the direction is OUT, INOUT (default: para_N)
name: para_1
- ...
where:
The direction of a parameter defines if a value should be:
| Direction | Statement Kind | Value passed to the statement | Value returned after execution |
|---|---|---|---|
| IN (default) | All DML | True | False |
| OUT | Procedure only | False | True |
| INOUT | Procedure only | True | True |
If one of the parameters has a OUT or INOUT direction, they are returned in a data resource.
The placeholder parameter syntax differs depending on your database.
| Notation | Database |
|---|---|
| ? | MySQL, SQL Server, Oracle, SQLite, … |
| $N | PostgreSQL (if not escaped) |
Parameterized Statements fall into 2 categories:
Table names and column names cannot be parameterized, only value (ie you can only parameterize values, not structural elements)
Not all kind of statement can be written as prepared statement.
You need to refer to your database documentation but generally, they are supported with DML statement (Select, Insert, Update, Merge, Call)