diff --git a/.translation-init b/.translation-init index 2e11c117a0..411f917197 100644 --- a/.translation-init +++ b/.translation-init @@ -1 +1 @@ -Translation initialization: 2025-10-21T15:20:01.505795 +Translation initialization: 2025-10-22T01:38:35.795002 diff --git a/docs/cn/sql-reference/10-sql-commands/00-ddl/15-variable/index.md b/docs/cn/sql-reference/10-sql-commands/00-ddl/15-variable/index.md index 54f32812d5..cebfeec4ab 100644 --- a/docs/cn/sql-reference/10-sql-commands/00-ddl/15-variable/index.md +++ b/docs/cn/sql-reference/10-sql-commands/00-ddl/15-variable/index.md @@ -1,22 +1,57 @@ --- -title: 变量(Variable) +title: SQL 变量 +sidebar_label: SQL 变量 --- -本文档全面介绍 Databend 中的变量操作,按功能分类以便查阅。 +SQL 变量允许你在会话中存储和管理临时数据,使脚本更具动态性和可重用性。 -## 变量管理 +## 变量命令 | 命令 | 描述 | |---------|-------------| -| [SET](set-variable.md) | 创建或修改会话/用户变量 | -| [UNSET](unset-variable.md) | 删除用户定义变量 | +| [SET VARIABLE](set-variable.md) | 创建或修改会话或用户变量。 | +| [UNSET VARIABLE](unset-variable.md) | 删除用户定义的变量。 | +| [SHOW VARIABLES](show-variables.md) | 显示系统和用户变量的当前值。 | -## 变量信息 +`SHOW VARIABLES` 命令还有一个对应的表函数,即 [`SHOW_VARIABLES`](../../../20-sql-functions/17-table-functions/show-variables.md),它以表格格式返回相同的信息,以便进行更丰富的筛选和查询。 -| 命令 | 描述 | -|---------|-------------| -| [SHOW VARIABLES](show-variables.md) | 显示系统及用户变量的当前值 | +## 使用变量进行查询 + +你可以在语句中引用变量,以进行动态值替换或在运行时构建对象名称。 + +### 使用 `$` 和 `getvariable()` 访问变量 + +使用 `$` 符号或 `getvariable()` 函数将变量值直接嵌入查询中。 + +```sql title='示例:' +-- 设置一个变量用作筛选值 +SET VARIABLE threshold = 100; + +-- 在查询中使用 $ 引用变量 +SELECT * FROM sales WHERE amount > $threshold; + +-- 或者,使用 getvariable() 函数 +SELECT * FROM sales WHERE amount > getvariable('threshold'); +``` + +### 使用 `IDENTIFIER` 访问对象 + +`IDENTIFIER` 关键字允许你引用名称存储在变量中的数据库对象,从而实现灵活的查询构建。(注意:BendSQL 尚不支持 `IDENTIFIER`。) + +```sql title='示例:' +-- 创建一个包含销售数据的表 +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 VARIABLE table_name = 'sales_data'; +SET VARIABLE column_name = 'sales_amount'; -:::note -Databend 的变量支持在会话内或跨会话存储和重用值,使脚本更具动态性与可重用性。 -::: \ No newline at end of file +-- 使用 IDENTIFIER 在查询中动态引用表和列 +SELECT region, IDENTIFIER($column_name) +FROM IDENTIFIER($table_name) +WHERE IDENTIFIER($column_name) > 4000; +``` \ No newline at end of file