Skip to content

Commit 2d82df9

Browse files
AI Translate 15-variable to Simplified-Chinese (#2873)
* [INIT] Start translation to Simplified-Chinese * 🌐 Translate index.md to Simplified-Chinese --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 063cf27 commit 2d82df9

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

.translation-init

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Translation initialization: 2025-10-21T15:20:01.505795
1+
Translation initialization: 2025-10-22T01:38:35.795002
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 变量
3+
sidebar_label: SQL 变量
34
---
45

5-
本文档全面介绍 Databend 中的变量操作,按功能分类以便查阅
6+
SQL 变量允许你在会话中存储和管理临时数据,使脚本更具动态性和可重用性
67

7-
## 变量管理
8+
## 变量命令
89

910
| 命令 | 描述 |
1011
|---------|-------------|
11-
| [SET](set-variable.md) | 创建或修改会话/用户变量 |
12-
| [UNSET](unset-variable.md) | 删除用户定义变量 |
12+
| [SET VARIABLE](set-variable.md) | 创建或修改会话或用户变量。 |
13+
| [UNSET VARIABLE](unset-variable.md) | 删除用户定义的变量。 |
14+
| [SHOW VARIABLES](show-variables.md) | 显示系统和用户变量的当前值。 |
1315

14-
## 变量信息
16+
`SHOW VARIABLES` 命令还有一个对应的表函数,即 [`SHOW_VARIABLES`](../../../20-sql-functions/17-table-functions/show-variables.md),它以表格格式返回相同的信息,以便进行更丰富的筛选和查询。
1517

16-
| 命令 | 描述 |
17-
|---------|-------------|
18-
| [SHOW VARIABLES](show-variables.md) | 显示系统及用户变量的当前值 |
18+
## 使用变量进行查询
19+
20+
你可以在语句中引用变量,以进行动态值替换或在运行时构建对象名称。
21+
22+
### 使用 `$``getvariable()` 访问变量
23+
24+
使用 `$` 符号或 `getvariable()` 函数将变量值直接嵌入查询中。
25+
26+
```sql title='示例:'
27+
-- 设置一个变量用作筛选值
28+
SET VARIABLE threshold = 100;
29+
30+
-- 在查询中使用 $ 引用变量
31+
SELECT * FROM sales WHERE amount > $threshold;
32+
33+
-- 或者,使用 getvariable() 函数
34+
SELECT * FROM sales WHERE amount > getvariable('threshold');
35+
```
36+
37+
### 使用 `IDENTIFIER` 访问对象
38+
39+
`IDENTIFIER` 关键字允许你引用名称存储在变量中的数据库对象,从而实现灵活的查询构建。(注意:BendSQL 尚不支持 `IDENTIFIER`。)
40+
41+
```sql title='示例:'
42+
-- 创建一个包含销售数据的表
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+
-- 为表名和列名设置变量
50+
SET VARIABLE table_name = 'sales_data';
51+
SET VARIABLE column_name = 'sales_amount';
1952

20-
:::note
21-
Databend 的变量支持在会话内或跨会话存储和重用值,使脚本更具动态性与可重用性。
22-
:::
53+
-- 使用 IDENTIFIER 在查询中动态引用表和列
54+
SELECT region, IDENTIFIER($column_name)
55+
FROM IDENTIFIER($table_name)
56+
WHERE IDENTIFIER($column_name) > 4000;
57+
```

0 commit comments

Comments
 (0)