|
1 | 1 | --- |
2 | | -title: Variable |
| 2 | +title: SQL Variables |
| 3 | +sidebar_label: SQL Variables |
3 | 4 | --- |
4 | 5 |
|
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. |
6 | 7 |
|
7 | | -## Variable Management |
| 8 | +## Variable Commands |
8 | 9 |
|
9 | 10 | | Command | Description | |
10 | 11 | |---------|-------------| |
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. | |
13 | 15 |
|
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. |
15 | 17 |
|
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'; |
19 | 52 |
|
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 | +``` |
0 commit comments