You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import FunctionDescription from '@site/src/components/FunctionDescription';
7
+
8
+
<FunctionDescriptiondescription="Introduced or updated: v1.2.833"/>
9
+
6
10
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.
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.
56
60
57
61
```sql
58
62
CREATE OR REPLACE PROCEDURE sp_with_declare()
59
63
RETURNS INT
60
64
LANGUAGE SQL
61
65
AS $$
62
66
DECLARE
63
-
counter :=0;
67
+
counter INT DEFAULT0;
64
68
BEGIN
65
69
counter := counter +5;
66
70
RETURN counter;
@@ -70,23 +74,26 @@ $$;
70
74
CALL PROCEDURE sp_with_declare();
71
75
```
72
76
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.
74
78
75
79
### Variables and Assignment
76
80
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`.
78
82
79
83
```sql
80
84
CREATE OR REPLACE PROCEDURE sp_demo_variables()
81
85
RETURNS FLOAT
82
86
LANGUAGE SQL
83
87
AS $$
84
88
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
87
93
88
94
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;
90
97
91
98
RETURN total;
92
99
END;
@@ -95,6 +102,8 @@ $$;
95
102
CALL PROCEDURE sp_demo_variables();
96
103
```
97
104
105
+
Referencing an uninitialized variable anywhere in a procedure raises error 3129.
106
+
98
107
### Variable Scope
99
108
100
109
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