You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/sql-reference/10-sql-commands/00-ddl/15-variable/set-variable.md
+54-19Lines changed: 54 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ import FunctionDescription from '@site/src/components/FunctionDescription';
5
5
6
6
<FunctionDescriptiondescription="Introduced or updated: v1.2.609"/>
7
7
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.
9
9
10
10
## Syntax
11
11
@@ -15,43 +15,78 @@ SET VARIABLE <variable_name> = <expression>
15
15
16
16
-- Set more than one variable
17
17
SET VARIABLE (<variable1>, <variable2>, ...) = (<expression1>, <expression2>, ...)
18
+
19
+
-- Set multiple variables from a query result
20
+
SET VARIABLE (<variable1>, <variable2>, ...) =<query>
18
21
```
19
22
23
+
## Accessing Variables
24
+
25
+
Variables can be accessed using the dollar sign syntax: `$variable_name`
26
+
20
27
## Examples
21
28
22
-
The following example sets a single variable:
29
+
### Setting a Single Variable
23
30
24
31
```sql
25
-
--sets variable a to the string 'databend'
32
+
--Sets variable a to the string 'databend'
26
33
SET VARIABLE a ='databend';
34
+
35
+
-- Access the variable
36
+
SELECT $a;
37
+
┌─────────┐
38
+
│ $a │
39
+
├─────────┤
40
+
│ databend│
41
+
└─────────┘
27
42
```
28
43
29
-
The following example sets a variable with the table name and uses IDENTIFIER to dynamically query the table based on that variable:
-- 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');
39
49
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
+
└────┴────┘
43
57
```
44
58
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
46
60
47
61
```sql
48
62
-- Sets variable a to 3 and b to 55
49
63
SET VARIABLE (a, b) = (SELECT3, 55);
64
+
65
+
-- Access the variables
66
+
SELECT $a, $b;
67
+
┌────┬────┐
68
+
│ $a │ $b │
69
+
├────┼────┤
70
+
│ 3 │ 55 │
71
+
└────┴────┘
50
72
```
51
73
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:
0 commit comments