Skip to content

Commit d95c4cc

Browse files
authored
docs: document procedure declare defaults (#2928)
1 parent e020464 commit d95c4cc

File tree

2 files changed

+32
-14
lines changed
  • docs
    • cn/sql-reference/30-stored-procedure-scripting
    • en/sql-reference/30-stored-procedure-scripting

2 files changed

+32
-14
lines changed

docs/cn/sql-reference/30-stored-procedure-scripting/index.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ title: 存储过程(Stored Procedure)与 SQL 脚本
33
slug: /stored-procedure-scripting/
44
---
55

6+
import FunctionDescription from '@site/src/components/FunctionDescription';
7+
8+
<FunctionDescription description="Introduced or updated: v1.2.833"/>
9+
610
Databend 中的存储过程(Stored Procedure)允许您将 SQL 逻辑打包在服务器上运行,并支持控制流(Control Flow)、变量(Variable)、游标(Cursor)和动态语句(Dynamic Statement)。本页面介绍如何创建存储过程以及编写驱动它们的内联脚本。
711

812
## 定义存储过程
@@ -52,15 +56,15 @@ CALL PROCEDURE convert_kg_to_lb(10);
5256

5357
### 声明部分
5458

55-
存储过程可以以可选的 `DECLARE` 块开始,在可执行部分之前初始化变量(Variable)。
59+
存储过程可以以可选的 `DECLARE` 块开始,在可执行部分之前初始化变量(Variable)。每个条目遵循与 `LET` 相同的语法:`name [<data_type>] [:= <expr> | DEFAULT <expr>]`。如果省略初始化器,变量必须在读取之前被赋值;过早引用会触发错误 3129。
5660

5761
```sql
5862
CREATE OR REPLACE PROCEDURE sp_with_declare()
5963
RETURNS INT
6064
LANGUAGE SQL
6165
AS $$
6266
DECLARE
63-
counter := 0;
67+
counter INT DEFAULT 0;
6468
BEGIN
6569
counter := counter + 5;
6670
RETURN counter;
@@ -70,23 +74,26 @@ $$;
7074
CALL PROCEDURE sp_with_declare();
7175
```
7276

73-
`DECLARE` 部分接受与 `LET` 相同的定义,包括 `RESULTSET``CURSOR` 声明。每项后使用分号。
77+
`DECLARE` 部分接受与 `LET` 相同的定义,包括可选的数据类型、`RESULTSET``CURSOR` 声明。每项后使用分号。
7478

7579
### 变量与赋值
7680

77-
使用 `LET` 声明变量(Variable)或常量(Constant)通过省略 `LET` 进行重新赋值。
81+
使用 `LET` 声明变量(Variable)或常量(Constant)。可以选择添加类型标注,并使用 `:=``DEFAULT` 关键字指定初始值。如果省略初始化器,变量必须在读取之前被赋值;提前引用会触发错误 3129。通过省略 `LET` 进行重新赋值。
7882

7983
```sql
8084
CREATE OR REPLACE PROCEDURE sp_demo_variables()
8185
RETURNS FLOAT
8286
LANGUAGE SQL
8387
AS $$
8488
BEGIN
85-
LET total := 100;
86-
LET rate := 0.07;
89+
LET total DECIMAL(10, 2) DEFAULT 100;
90+
LET rate FLOAT := 0.07;
91+
LET surcharge FLOAT := NULL; -- 使用前显式初始化
92+
LET tax FLOAT DEFAULT rate; -- DEFAULT 可以引用已经初始化的变量
8793

8894
total := total * rate; -- 乘以比率
89-
total := total + 5; -- 不使用 LET 重新赋值
95+
total := total + COALESCE(surcharge, 5); -- 不使用 LET 重新赋值
96+
total := total + tax;
9097

9198
RETURN total;
9299
END;
@@ -95,6 +102,8 @@ $$;
95102
CALL PROCEDURE sp_demo_variables();
96103
```
97104

105+
在存储过程的任何位置引用未初始化的变量都会触发错误 3129。
106+
98107
### 变量作用域
99108

100109
变量(Variable)的作用域限定在封闭块内。内部块可以遮蔽外部绑定,当块退出时恢复外部值。

docs/en/sql-reference/30-stored-procedure-scripting/index.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ title: Stored Procedure & SQL Scripting
33
slug: /stored-procedure-scripting/
44
---
55

6+
import FunctionDescription from '@site/src/components/FunctionDescription';
7+
8+
<FunctionDescription description="Introduced or updated: v1.2.833"/>
9+
610
Stored procedures in Databend let you package SQL logic that runs on the server with access to control flow, variables, cursors, and dynamic statements. This page explains how to create procedures and write the inline scripting that powers them.
711

812
## Defining a Procedure
@@ -52,15 +56,15 @@ CALL PROCEDURE convert_kg_to_lb(10);
5256

5357
### Declare Section
5458

55-
Stored procedures can start with an optional `DECLARE` block to initialize variables before the executable section.
59+
Stored procedures can start with an optional `DECLARE` block to initialize variables before the executable section. Each entry in the block follows the same syntax as `LET`: `name [<data_type>] [:= <expr> | DEFAULT <expr>]`. When you omit the initializer, the variable must be assigned before it is read; referencing it too early raises error 3129.
5660

5761
```sql
5862
CREATE OR REPLACE PROCEDURE sp_with_declare()
5963
RETURNS INT
6064
LANGUAGE SQL
6165
AS $$
6266
DECLARE
63-
counter := 0;
67+
counter INT DEFAULT 0;
6468
BEGIN
6569
counter := counter + 5;
6670
RETURN counter;
@@ -70,23 +74,26 @@ $$;
7074
CALL PROCEDURE sp_with_declare();
7175
```
7276

73-
The `DECLARE` section accepts the same definitions as `LET`, including `RESULTSET` and `CURSOR` declarations. Use a semicolon after each item.
77+
The `DECLARE` section accepts the same definitions as `LET`, including optional data types, `RESULTSET`, and `CURSOR` declarations. Use a semicolon after each item.
7478

7579
### Variables and Assignment
7680

77-
Use `LET` to declare variables or constants, and reassign by omitting `LET`.
81+
Use `LET` to declare variables or constants. You can optionally provide a type annotation and an initializer with either `:=` or the `DEFAULT` keyword. Without an initializer, the variable must be assigned before it is read; referencing it beforehand raises error 3129. Reassign by omitting `LET`.
7882

7983
```sql
8084
CREATE OR REPLACE PROCEDURE sp_demo_variables()
8185
RETURNS FLOAT
8286
LANGUAGE SQL
8387
AS $$
8488
BEGIN
85-
LET total := 100;
86-
LET rate := 0.07;
89+
LET total DECIMAL(10, 2) DEFAULT 100;
90+
LET rate FLOAT := 0.07;
91+
LET surcharge FLOAT := NULL; -- Explicitly initialize before use
92+
LET tax FLOAT DEFAULT rate; -- DEFAULT can reference initialized variables
8793

8894
total := total * rate; -- Multiply by the rate
89-
total := total + 5; -- Reassign without LET
95+
total := total + COALESCE(surcharge, 5); -- Reassign without LET
96+
total := total + tax;
9097

9198
RETURN total;
9299
END;
@@ -95,6 +102,8 @@ $$;
95102
CALL PROCEDURE sp_demo_variables();
96103
```
97104

105+
Referencing an uninitialized variable anywhere in a procedure raises error 3129.
106+
98107
### Variable Scope
99108

100109
Variables are scoped to the enclosing block. Inner blocks can shadow outer bindings, and the outer value is restored when the block exits.

0 commit comments

Comments
 (0)