---json
{
"page_id": "glyplljqekc7k4cuqdxva"
}
---
====== JDBC Escape Syntax ======
===== About =====
''JDBC Escape Syntax'' is Java syntaxes sugar on top of SQL.
It's a templating function that permits to translate SQL clause into the different [[database|database]] SQL flavor.
In the examples below, we use the question mark ''?''. It's known as a [[:docs:system:database:sql_parameterized_statement|parameter]]
===== Stored Procedure Example =====
For example, you can call a [[:docs:resource:sql_stored_procedure|stored procedure]] with this escaped SQL syntax
{call procedure_name (?, ?)}
and it will be translated by the database (not by tabulify) to:
* in Oracle
BEGIN procedure_name(?, ?); END;
* in SQL Server,
EXEC procedure_name ?, ?
* in PostgreSQL
CALL procedure_name(?, ?)
===== Syntaxes =====
==== Stored Procedure ====
The escape syntax to call a [[:docs:resource:sql_stored_procedure|stored procedure]] is:
```sql
{call [()]}
{call procedure_name (?, ?)}
```
or, where a procedure returns a result parameter:
```sql
{? = call [()]}
{? = call procedure_name (?, ?)}
```
==== Scalar function ====
The escape syntax to access a [[:docs:resource:sql_function|scalar function]] is:
{fn (argument list)}
For example,
* ''concat'' with two arguments to be concatenated:
-- called as procedure
{fn concat("Hello", " ", "World")}
-- called with a select
select {fn concat("Hello", " ", "World")}
* Gets the name of the current database user:
-- called as procedure
{fn user()}
-- called with a select
select {fn user()}
* ''Substring''
{fn SUBSTRING(text, 1, 10)}
* ''Upper''
{fn upper( ? ) }
* All other functions are listed in the [[#specification|JDBC specification section Scalar function]]
They get translated into database-specific functions
"Hello" || " " || "World" -- Oracle concat
SUBSTR(text, 1, 10) -- Oracle substring
"Hello" + " " + "World" -- SQL Server concat
SUBSTRING(text, 1, 10) -- SQL Server substring
==== Date/Time literals ====
{d 'yyyy-mm-dd'} -- date
{t 'hh:mm:ss'} -- time
{ts 'yyyy-mm-dd hh:mm:ss.f...'} -- timestamp
Example:
select {d '2023-12-25'} // Date
select {t '14:30:00'} // Time
select {ts '2023-12-25 14:30:00'} // Timestamp
This syntax gets translated to database-specific format
DATE '2023-12-25' -- Oracle Date
'2023-12-25' -- SQL Server Date
==== Outer Join ====
The escape syntax for an outer join is:
{oj }
where ''outer-join'' has the form:
table {LEFT|RIGHT|FULL} OUTER JOIN {table | } ON search-condition
Example:
SELECT * FROM {oj TABLE1 LEFT OUTER JOIN TABLE2 ON DEPT_NO = 003420930}
==== LIKE Escape ====
The following syntax can be used at the end of a LIKE predicate:
{escape ''}
Example:
SELECT name FROM Identifiers WHERE Id LIKE '\\_%' {escape '\\'}
==== Limit Escape ====
The escape syntax for limiting the number of rows returned by a query is:
{limit }
where the format for the ''limit clause'' is:
rows [offset row_offset]
Example:
SELECT * FROM TABLE1 WHERE F1 >100 {limit 20}
===== Specification =====
More information can be found in the [[https://www.jcp.org/en/jsr/detail?id=221|JDBC Specification JSR 221]], section ''13.4 Escape Syntax'' ([[https://download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/jdbc4.1-fr-spec.pdf|Direct PDF Link]])