Skip to content

Commit 063cf27

Browse files
authored
docs: merge SQL variables overview into DDL section (#2872)
1 parent db217fb commit 063cf27

File tree

3 files changed

+51
-69
lines changed

3 files changed

+51
-69
lines changed

docs/en/sql-reference/00-sql-reference/41-sql-variables.md

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
11
---
2-
title: Variable
2+
title: SQL Variables
3+
sidebar_label: SQL Variables
34
---
45

5-
This page provides a comprehensive overview of Variable operations in Databend, organized by functionality for easy reference.
6+
SQL variables allow you to store and manage temporary data within a session, making scripts more dynamic and reusable.
67

7-
## Variable Management
8+
## Variable Commands
89

910
| Command | Description |
1011
|---------|-------------|
11-
| [SET](set-variable.md) | Creates or modifies a session or user variable |
12-
| [UNSET](unset-variable.md) | Removes a user-defined variable |
12+
| [SET VARIABLE](set-variable.md) | Creates or modifies a session or user variable. |
13+
| [UNSET VARIABLE](unset-variable.md) | Removes a user-defined variable. |
14+
| [SHOW VARIABLES](show-variables.md) | Displays current values of system and user variables. |
1315

14-
## Variable Information
16+
The SHOW VARIABLES command also has a table function counterpart, [`SHOW_VARIABLES`](../../../20-sql-functions/17-table-functions/show-variables.md), which returns the same information in a tabular format for richer filtering and querying.
1517

16-
| Command | Description |
17-
|---------|-------------|
18-
| [SHOW VARIABLES](show-variables.md) | Displays current values of system and user variables |
18+
## Querying with Variables
19+
20+
You can reference variables in statements for dynamic value substitution or to build object names at runtime.
21+
22+
### Accessing Variables with `$` and `getvariable()`
23+
24+
Use the `$` symbol or the `getvariable()` function to embed variable values directly in a query.
25+
26+
```sql title='Example:'
27+
-- Set a variable to use as a filter value
28+
SET VARIABLE threshold = 100;
29+
30+
-- Use the variable in a query with $
31+
SELECT * FROM sales WHERE amount > $threshold;
32+
33+
-- Alternatively, use the getvariable() function
34+
SELECT * FROM sales WHERE amount > getvariable('threshold');
35+
```
36+
37+
### Accessing Objects with `IDENTIFIER`
38+
39+
The `IDENTIFIER` keyword lets you reference database objects whose names are stored in variables, enabling flexible query construction. (Note: BendSQL does not yet support `IDENTIFIER`.)
40+
41+
```sql title='Example:'
42+
-- Create a table with sales data
43+
CREATE TABLE sales_data (region TEXT, sales_amount INT, month TEXT) AS
44+
SELECT 'North', 5000, 'January' UNION ALL
45+
SELECT 'South', 3000, 'January';
46+
47+
select * from sales_data;
48+
49+
-- Set variables for the table name and column name
50+
SET VARIABLE table_name = 'sales_data';
51+
SET VARIABLE column_name = 'sales_amount';
1952

20-
:::note
21-
Variables in Databend allow you to store and reuse values within a session or across sessions, making scripts more dynamic and reusable.
22-
:::
53+
-- Use IDENTIFIER to dynamically reference the table and column in the query
54+
SELECT region, IDENTIFIER($column_name)
55+
FROM IDENTIFIER($table_name)
56+
WHERE IDENTIFIER($column_name) > 4000;
57+
```

site-redirects.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const siteRedirects = [
1919
from: '/guides/access-data-lake/delta',
2020
to: '/sql/sql-reference/table-engines/delta'
2121
},
22+
{
23+
from: '/sql/sql-reference/sql-variables',
24+
to: '/sql/sql-commands/ddl/variable/'
25+
},
2226
// AI Functions redirects - functions moved to external implementation
2327
{
2428
from: '/sql/sql-functions/ai-functions/ai-cosine-distance',

0 commit comments

Comments
 (0)