|
| 1 | +--- |
| 2 | +title: Stored Procedure |
| 3 | +--- |
| 4 | + |
| 5 | +A stored procedure is a set of executable commands or logic blocks stored within the database, written in SQL or other programming languages, designed to be reusable for efficiently performing specific tasks or operations. |
| 6 | + |
| 7 | +## Supported Languages |
| 8 | + |
| 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. |
| 10 | + |
| 11 | +### SQL Scripting |
| 12 | + |
| 13 | +In Databend, stored procedures support a rich set of features to allow users to define complex operations through SQL scripting. Here are the supported capabilities: |
| 14 | + |
| 15 | +- **Variable Declaration**: Variables can be declared using the `LET` keyword, followed by the variable name, an optional type, and the initial value. |
| 16 | + |
| 17 | + ```sql title='Examples:' |
| 18 | + LET x := 100; |
| 19 | + ``` |
| 20 | + |
| 21 | +- **Query Execution**: SQL queries can be executed within the script, and results can be stored in variables or result sets. |
| 22 | + |
| 23 | + ```sql title='Examples:' |
| 24 | + LET result RESULTSET := SELECT * FROM t1; |
| 25 | + ``` |
| 26 | + |
| 27 | +- **Control Flow Constructs** |
| 28 | + - **FOR Loop**: Iterates over a range or a result set. |
| 29 | + |
| 30 | + ```sql title='Examples:' |
| 31 | + FOR i IN 1..10 DO ... END FOR; |
| 32 | + ``` |
| 33 | + |
| 34 | + - **WHILE Loop**: Executes a block of code as long as a specified condition is true. |
| 35 | + |
| 36 | + ```sql title='Examples:' |
| 37 | + WHILE condition DO ... END WHILE; |
| 38 | + ``` |
| 39 | + |
| 40 | + - **REPEAT Loop**: Executes a block of code until a condition is met. |
| 41 | + |
| 42 | + ```sql title='Examples:' |
| 43 | + REPEAT ... UNTIL condition END REPEAT; |
| 44 | + ``` |
| 45 | + |
| 46 | + - **LOOP**: Executes a block of code indefinitely until a `BREAK` statement is encountered. |
| 47 | + |
| 48 | + ```sql title='Examples:' |
| 49 | + LOOP ... END LOOP; |
| 50 | + ``` |
| 51 | + |
| 52 | + - **CASE Statement**: Allows conditional execution of code blocks based on different conditions. |
| 53 | + |
| 54 | + ```sql title='Examples:' |
| 55 | + CASE [operand] |
| 56 | + WHEN condition1 THEN ... |
| 57 | + WHEN condition2 THEN ... |
| 58 | + ELSE ... |
| 59 | + END; |
| 60 | + ``` |
| 61 | + |
| 62 | + - **IF Statement**: Executes a block of code based on a condition. |
| 63 | + |
| 64 | + ```sql title='Examples:' |
| 65 | + IF condition THEN ... |
| 66 | + ELSEIF condition THEN ... |
| 67 | + ELSE ... |
| 68 | + END IF; |
| 69 | + ``` |
| 70 | + |
| 71 | +- **RETURN**: Returns from the script with an optional value. |
| 72 | + |
| 73 | + ```sql title='Examples:' |
| 74 | + RETURN [expr]; |
| 75 | + ``` |
| 76 | + |
| 77 | +- **RETURN TABLE**: Returns from the script with a table result. |
| 78 | + |
| 79 | + ```sql title='Examples:' |
| 80 | + RETURN TABLE(result_set_name | SELECT ...); |
| 81 | + ``` |
| 82 | + |
| 83 | +- **Comments** |
| 84 | + - **Single-line comments**: `-- comment` |
| 85 | + - **Multi-line comments**: `/* comment */` |
| 86 | + |
| 87 | +## Managing Stored Procedures |
| 88 | + |
| 89 | +Databend offers a range of commands for managing stored procedures. For more details, see [Stored Procedure](/sql/sql-commands/ddl/procedure/). |
| 90 | + |
| 91 | +## Usage Example |
| 92 | + |
0 commit comments