Skip to content

Commit 65591aa

Browse files
committed
Revert "Revert "added""
This reverts commit 6f042a7.
1 parent e4f1bb5 commit 65591aa

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

docs/en/guides/54-query/08-stored-procedure.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A stored procedure is a set of executable commands or logic blocks stored within
66

77
## Supported Languages
88

9-
**Databend currently supports [SQL scripting](#sql-scripting) only**. Using SQL scripting, users can define procedures with control flow constructs like loops (FOR, WHILE, REPEAT) and conditionals (IF, CASE), enabling complex logic and effective multi-step operations.
9+
**Databend currently supports [SQL Scripting](#sql-scripting) only**. Using SQL scripting, users can define procedures with control flow constructs like loops (FOR, WHILE, REPEAT) and conditionals (IF, CASE), enabling complex logic and effective multi-step operations.
1010

1111
### SQL Scripting
1212

@@ -84,9 +84,57 @@ In Databend, stored procedures support a rich set of features to allow users to
8484
- **Single-line comments**: `-- comment`
8585
- **Multi-line comments**: `/* comment */`
8686

87+
## Limitations
88+
89+
The following limitations apply when working with stored procedures:
90+
91+
- Stored procedures are an experimental feature. Before working with them, set `enable_experimental_procedure` to 1;
92+
93+
```sql
94+
SET enable_experimental_procedure = 1;
95+
```
96+
97+
- Stored procedures return results as strings, regardless of the specified return type, without enforcing the declared type on the returned value.
98+
8799
## Managing Stored Procedures
88100

89101
Databend offers a range of commands for managing stored procedures. For more details, see [Stored Procedure](/sql/sql-commands/ddl/procedure/).
90102

91103
## Usage Example
92104

105+
Suppose we want to calculate the sum of all even numbers within a given range. This stored procedure accepts a starting value start_val and an ending value end_val and calculates the sum of all even numbers within this range.
106+
107+
```sql
108+
SET enable_experimental_procedure = 1;
109+
110+
CREATE PROCEDURE sum_even_numbers(start_val UInt8, end_val UInt8)
111+
RETURNS UInt8 NOT NULL
112+
LANGUAGE SQL
113+
COMMENT='Calculate the sum of all even numbers'
114+
AS $$
115+
BEGIN
116+
LET sum := 0;
117+
FOR i IN start_val TO end_val DO
118+
IF i % 2 = 0 THEN
119+
sum := sum + i;
120+
END IF;
121+
END FOR;
122+
123+
RETURN sum;
124+
END;
125+
$$;
126+
```
127+
128+
If we want to calculate the sum of all even numbers from 1 to 10, we can call the procedure as follows:
129+
130+
```sql
131+
CALL PROCEDURE sum_even_numbers(1, 10);
132+
133+
-- Result: 2 + 4 + 6 + 8 + 10 = 30
134+
┌────────┐
135+
│ Result │
136+
├────────┤
137+
│ 30 │
138+
└────────┘
139+
```
140+

docs/en/sql-reference/10-sql-commands/00-ddl/18-procedure/create-procedure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ $$;
2727
| `<procedure_name>` | Name of the procedure. |
2828
| `<parameter_name> <data_type>` | Input parameters (optional), each with a specified data type. Multiple parameters can be defined and separated by commas. |
2929
| `RETURNS <return_data_type> [NOT NULL]` | Specifies the data type of the return value. `NOT NULL` ensures the returned value cannot be NULL. |
30-
| `LANGUAGE` | Specifies the language in which the procedure body is written. Currently, only `SQL` is supported. |
30+
| `LANGUAGE` | Specifies the language in which the procedure body is written. Currently, only `SQL` is supported. For details, see [SQL Scripting](/guides/query/stored-procedure#sql-scripting). |
3131
| `COMMENT` | Optional text describing the procedure. |
3232
| `AS ...` | Encloses the procedure body, which contains SQL statements, variable declarations, loops, and a RETURN statement. |
3333

0 commit comments

Comments
 (0)