Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions docs/en/sql-reference/00-sql-reference/41-sql-variables.md

This file was deleted.

59 changes: 47 additions & 12 deletions docs/en/sql-reference/10-sql-commands/00-ddl/15-variable/index.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
---
title: Variable
title: SQL Variables
sidebar_label: SQL Variables
---

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

## Variable Management
## Variable Commands

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

## Variable Information
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.

| Command | Description |
|---------|-------------|
| [SHOW VARIABLES](show-variables.md) | Displays current values of system and user variables |
## Querying with Variables

You can reference variables in statements for dynamic value substitution or to build object names at runtime.

### Accessing Variables with `$` and `getvariable()`

Use the `$` symbol or the `getvariable()` function to embed variable values directly in a query.

```sql title='Example:'
-- Set a variable to use as a filter value
SET VARIABLE threshold = 100;

-- Use the variable in a query with $
SELECT * FROM sales WHERE amount > $threshold;

-- Alternatively, use the getvariable() function
SELECT * FROM sales WHERE amount > getvariable('threshold');
```

### Accessing Objects with `IDENTIFIER`

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`.)

```sql title='Example:'
-- Create a table with sales data
CREATE TABLE sales_data (region TEXT, sales_amount INT, month TEXT) AS
SELECT 'North', 5000, 'January' UNION ALL
SELECT 'South', 3000, 'January';

select * from sales_data;

-- Set variables for the table name and column name
SET VARIABLE table_name = 'sales_data';
SET VARIABLE column_name = 'sales_amount';

:::note
Variables in Databend allow you to store and reuse values within a session or across sessions, making scripts more dynamic and reusable.
:::
-- Use IDENTIFIER to dynamically reference the table and column in the query
SELECT region, IDENTIFIER($column_name)
FROM IDENTIFIER($table_name)
WHERE IDENTIFIER($column_name) > 4000;
```
4 changes: 4 additions & 0 deletions site-redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const siteRedirects = [
from: '/guides/access-data-lake/delta',
to: '/sql/sql-reference/table-engines/delta'
},
{
from: '/sql/sql-reference/sql-variables',
to: '/sql/sql-commands/ddl/variable/'
},
// AI Functions redirects - functions moved to external implementation
{
from: '/sql/sql-functions/ai-functions/ai-cosine-distance',
Expand Down
Loading