Skip to content

Commit 0e9e27d

Browse files
srielaugengliangwang
authored andcommitted
[SPARK-53766][DOC] Improve execute immediate docs
### What changes were proposed in this pull request? Improve documentation for EXECUTE IMMEDIATE ### Why are the changes needed? EXECUTE IMMEDIATE has been reworked to support more inputs. The existing docs are also a bit terse. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? Build the docs ### Was this patch authored or co-authored using generative AI tooling? No Closes #52494 from srielau/execute-immediate-docs. Authored-by: Serge Rielau <[email protected]> Signed-off-by: Gengliang Wang <[email protected]>
1 parent d5729f0 commit 0e9e27d

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

docs/sql-ref-syntax-aux-exec-imm.md

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,65 +21,84 @@ license: |
2121

2222
### Description
2323

24-
Executes a sql statement provided as a `STRING`, optionally passing `arg_exprN` to parameter markers and assigning the results to `var_nameN`.
24+
Executes a SQL statement provided as a `STRING`.
25+
The statement optionally passes arguments to parameter markers and assigns the results to variables.
2526

2627
### Syntax
2728

2829
```sql
2930
EXECUTE IMMEDIATE sql_string
30-
[ INTO var_name [, …] ]
31-
[ USING { (arg_expr [ AS ] [alias] [, …] ) | arg_expr [ AS ] [alias] [, …] } ]
31+
[ INTO var_name [, ...] ]
32+
[ USING { arg_expr [ AS ] [alias] } [, ...] ]
3233
```
3334

35+
For compatibility with other SQL dialects, `EXECUTE IMMEDIATE` also supports `USING ( { arg_expr [ AS ] [alias] } [, ...] )`
36+
3437
### Parameters
3538

3639
* **sql_string**
3740

38-
A STRING expression producing a well-formed SQL statement.
41+
A constant expression of type `STRING`, producing a well-formed SQL statement.
42+
43+
* **INTO var_name [, ...]**
44+
45+
Optionally returns the results of a single row query into SQL variables.
46+
If the query returns no rows the result is `NULL`.
47+
48+
If the statement is not a query, Spark raises `INVALID_STATEMENT_FOR_EXECUTE_INTO` error.
49+
50+
If the query returns more than one row, Spark raises `ROW_SUBQUERY_TOO_MANY_ROWS` error.
3951

40-
* **INTO var_name [, …]**
52+
* **var_name**
4153

42-
Optionally returns the results of a single row query into SQL variables.
43-
If the query returns no rows the result is NULL.
44-
- `var_name`
4554
A SQL variable. A variable may not be referenced more than once.
4655

47-
* **USING arg_expr [, …]**
56+
* **USING { arg_expr [ AS ] [alias] } [, ...]**
4857

49-
Optionally, if sql_string contains parameter markers, binds in values to the parameters.
50-
- `arg_expr`
51-
An expression that binds to a parameter marker.
52-
If the parameter markers are unnamed the binding is by position.
53-
For unnamed parameter markers, binding is by name.
54-
- `alias`
55-
Overrides the name used to bind `arg_expr` to a named parameter marker
58+
Optionally, if `sql_string` contains parameter markers, binds in values to the parameters.
5659

57-
Each named parameter marker must be matched once. Not all arg_expr must be matched.
60+
* **arg_expr**
5861

62+
A constant expression that binds to a parameter marker.
63+
If the parameter markers are unnamed, the binding is by position.
64+
For named parameter markers, binding is by name.
65+
66+
* **alias**
67+
68+
Overrides the name used to bind `arg_expr` to a named parameter marker.
69+
Each named parameter marker must be matched once. Not all `arg_expr` must be matched.
5970

6071
### Examples
6172

6273
```sql
6374
-- A self-contained execution using a literal string
64-
EXECUTE IMMEDIATE 'SELECT SUM(col1) FROM VALUES(?), (?)' USING 5, 6;
75+
EXECUTE IMMEDIATE 'SELECT SUM(c1) FROM VALUES(?), (?) AS t(c1)' USING 5, 6;
6576
11
6677

6778
-- A SQL string composed in a SQL variable
68-
DECLARE sqlStr = 'SELECT SUM(col1) FROM VALUES(?), (?)';
79+
DECLARE sqlStr = 'SELECT SUM(c1) FROM VALUES(?), (?) AS t(c1)';
6980
DECLARE arg1 = 5;
7081
DECLARE arg2 = 6;
7182
EXECUTE IMMEDIATE sqlStr USING arg1, arg2;
7283
11
7384

7485
-- Using the INTO clause
7586
DECLARE sum INT;
87+
DECLARE sqlStr = 'SELECT SUM(c1) FROM VALUES(:first), (:second) AS t(c1)';
7688
EXECUTE IMMEDIATE sqlStr INTO sum USING arg1, arg2;
7789
SELECT sum;
7890
11
7991

8092
-- Using named parameter markers
81-
SET VAR sqlStr = 'SELECT SUM(col1) FROM VALUES(:first), (:second)';
82-
EXECUTE IMMEDIATE sqlStr INTO sum USING 5 AS first, arg2 AS second;
93+
DECLARE sum INT;
94+
DECLARE sqlStr = 'SELECT SUM(c1) FROM VALUES(:first), (:second) AS t(c1)';
95+
EXECUTE IMMEDIATE sqlStr INTO sum USING (5 AS first, arg2 AS second);
8396
SELECT sum;
8497
11
98+
99+
-- Using constant expressions
100+
DECLARE foo = 'sum';
101+
EXECUTE IMMEDIATE 'SELECT ' || foo || '(c1) FROM VALUES(?), (?) AS t(c1)'
102+
USING 5 + 6, 7 + length('hello');
103+
33
85104
```

docs/sql-ref-syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ ability to generate logical and physical plan for a given query using
101101
* [DESCRIBE FUNCTION](sql-ref-syntax-aux-describe-function.html)
102102
* [DESCRIBE QUERY](sql-ref-syntax-aux-describe-query.html)
103103
* [DESCRIBE TABLE](sql-ref-syntax-aux-describe-table.html)
104+
* [EXECUTE IMMEDIATE](sql-ref-syntax-aux-exec-imm.html)
104105
* [LIST FILE](sql-ref-syntax-aux-resource-mgmt-list-file.html)
105106
* [LIST JAR](sql-ref-syntax-aux-resource-mgmt-list-jar.html)
106107
* [REFRESH](sql-ref-syntax-aux-cache-refresh.html)
@@ -109,7 +110,6 @@ ability to generate logical and physical plan for a given query using
109110
* [RESET](sql-ref-syntax-aux-conf-mgmt-reset.html)
110111
* [SET](sql-ref-syntax-aux-conf-mgmt-set.html)
111112
* [SET VAR](sql-ref-syntax-aux-set-var.html)
112-
* [EXECUTE IMMEDIATE](sql-ref-syntax-aux-exec-imm.html)
113113
* [SHOW COLUMNS](sql-ref-syntax-aux-show-columns.html)
114114
* [SHOW CREATE TABLE](sql-ref-syntax-aux-show-create-table.html)
115115
* [SHOW DATABASES](sql-ref-syntax-aux-show-databases.html)

0 commit comments

Comments
 (0)