Skip to content

Commit 0896c08

Browse files
authored
enhance SET VARIABLE documentation with detailed examples
1 parent 3c4c4ae commit 0896c08

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

docs/en/sql-reference/10-sql-commands/00-ddl/15-variable/set-variable.md

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import FunctionDescription from '@site/src/components/FunctionDescription';
55

66
<FunctionDescription description="Introduced or updated: v1.2.609"/>
77

8-
Sets the value of one or more SQL variables within a session. The values can be simple constants, expressions, query results, or database objects.
8+
Sets the value of one or more SQL variables within a session. The values can be simple constants, expressions, query results, or database objects. Variables persist for the duration of your session and can be used in subsequent queries.
99

1010
## Syntax
1111

@@ -15,43 +15,78 @@ SET VARIABLE <variable_name> = <expression>
1515

1616
-- Set more than one variable
1717
SET VARIABLE (<variable1>, <variable2>, ...) = (<expression1>, <expression2>, ...)
18+
19+
-- Set multiple variables from a query result
20+
SET VARIABLE (<variable1>, <variable2>, ...) = <query>
1821
```
1922

23+
## Accessing Variables
24+
25+
Variables can be accessed using the dollar sign syntax: `$variable_name`
26+
2027
## Examples
2128

22-
The following example sets a single variable:
29+
### Setting a Single Variable
2330

2431
```sql
25-
-- sets variable a to the string 'databend'
32+
-- Sets variable a to the string 'databend'
2633
SET VARIABLE a = 'databend';
34+
35+
-- Access the variable
36+
SELECT $a;
37+
┌─────────┐
38+
│ $a │
39+
├─────────┤
40+
│ databend│
41+
└─────────┘
2742
```
2843

29-
The following example sets a variable with the table name and uses IDENTIFIER to dynamically query the table based on that variable:
44+
### Setting Multiple Variables
3045

3146
```sql
32-
CREATE TABLE monthly_sales(empid INT, amount INT, month TEXT) AS SELECT 1, 2, '3';
33-
34-
-- Set a variable 't' to the name of the table 'monthly_sales'
35-
SET VARIABLE t = 'monthly_sales';
36-
37-
-- Use IDENTIFIER to dynamically reference the table name stored in the variable 't'
38-
SELECT * FROM IDENTIFIER($t);
47+
-- Sets variable x to 'xx' and y to 'yy'
48+
SET VARIABLE (x, y) = ('xx', 'yy');
3949

40-
empid|amount|month|
41-
-----+------+-----+
42-
1| 2|3 |
50+
-- Access multiple variables
51+
SELECT $x, $y;
52+
┌────┬────┐
53+
│ $x │ $y │
54+
├────┼────┤
55+
│ xx │ yy │
56+
└────┴────┘
4357
```
4458

45-
The following example sets multiple variables from a query in a single statement. The query must return exactly one row, with the same number of values as the variables being set.
59+
### Setting Variables from Query Results
4660

4761
```sql
4862
-- Sets variable a to 3 and b to 55
4963
SET VARIABLE (a, b) = (SELECT 3, 55);
64+
65+
-- Access the variables
66+
SELECT $a, $b;
67+
┌────┬────┐
68+
│ $a │ $b │
69+
├────┼────┤
70+
355
71+
└────┴────┘
5072
```
5173

52-
The following example sets multiple variables to constants:
74+
### Dynamic Table References
75+
76+
Variables can be used with the `IDENTIFIER()` function to dynamically reference database objects:
5377

5478
```sql
55-
-- Sets variable x to 'xx' and y to 'yy'
56-
SET VARIABLE (x, y) = ('xx', 'yy');
57-
```
79+
-- Create a sample table
80+
CREATE OR REPLACE TABLE monthly_sales(empid INT, amount INT, month TEXT) AS SELECT 1, 2, '3';
81+
82+
-- Set a variable 't' to the name of the table 'monthly_sales'
83+
SET VARIABLE t = 'monthly_sales';
84+
85+
-- Use IDENTIFIER to dynamically reference the table name stored in the variable 't'
86+
SELECT * FROM IDENTIFIER($t);
87+
┌───────┬────────┬───────┐
88+
│ empid │ amount │ month │
89+
├───────┼────────┼───────┤
90+
123
91+
└───────┴────────┴───────┘
92+
```

0 commit comments

Comments
 (0)