Skip to content

Commit b7b3055

Browse files
committed
Revert "Revert "fix comments""
This reverts commit 33e932e.
1 parent 65591aa commit b7b3055

File tree

4 files changed

+180
-78
lines changed

4 files changed

+180
-78
lines changed

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

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +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.
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 */`
9+
**Databend currently supports [SQL Scripting](/sql/sql-reference/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.
8610

8711
## Limitations
8812

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: SQL Scripting
3+
---
4+
5+
This page outlines the SQL scripting options available in Databend. You can use SQL scripting with either of the following query methods:
6+
7+
- [Stored Procedure](/guides/query/stored-procedure)
8+
- [Execute Immediate](/sql/sql-commands/administration-cmds/execute-immediate)
9+
10+
### Variable Declaration
11+
12+
Variables can be declared using the `LET` keyword, followed by the variable name, an optional type, and the initial value.
13+
14+
```sql title='Examples:'
15+
LET x := 100;
16+
```
17+
18+
### Query Execution
19+
20+
SQL queries can be executed within the script, and results can be stored in variables or result sets.
21+
22+
```sql title='Examples:'
23+
LET result RESULTSET := SELECT * FROM t1;
24+
```
25+
26+
### Control Flow Constructs
27+
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
72+
73+
Returns from the script with an optional value.
74+
75+
```sql title='Examples:'
76+
RETURN [expr];
77+
```
78+
79+
### RETURN TABLE
80+
81+
Returns from the script with a table result as a String column.
82+
83+
```sql title='Examples:'
84+
EXECUTE IMMEDIATE $$
85+
BEGIN
86+
CREATE OR REPLACE TABLE t1 (a INT, b FLOAT, c STRING);
87+
INSERT INTO t1 VALUES (1, 2.0, '3');
88+
RETURN TABLE(select * from t1);
89+
END;
90+
$$;
91+
92+
┌─────────────────────────────────────────────┐
93+
│ Result │
94+
│ String │
95+
├─────────────────────────────────────────────┤
96+
│ ┌─────────────────────────────────────────┐ │
97+
│ │ a │ b │ c │ │
98+
│ │ Int32 NULL │ Float32 NULL │ String NULL │ │
99+
│ ├────────────┼──────────────┼─────────────┤ │
100+
│ │ 1 │ 2 │ '3' │ │
101+
│ └─────────────────────────────────────────┘ │
102+
└─────────────────────────────────────────────┘
103+
```
104+
105+
### Comments
106+
107+
- **Single-line comments**: `-- comment`
108+
- **Multi-line comments**: `/* comment */`

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ LANGUAGE <language>
1717
AS $$
1818
BEGIN
1919
<procedure_body>
20-
RETURN <return_value>;
20+
RETURN <return_value>; -- Use to return a single value
21+
-- OR
22+
RETURN TABLE(<select_query>); -- Use to return a table
2123
END;
2224
$$;
2325
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: EXECUTE IMMEDIATE
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.452"/>
7+
8+
Executes a SQL script. For how to write SQL scripts for Databend, see [SQL Scripting](/sql/sql-reference/sql-scripting).
9+
10+
## Syntax
11+
12+
```sql
13+
EXECUTE IMMEDIATE $$
14+
BEGIN
15+
<procedure_body>
16+
RETURN <return_value>; -- Use to return a single value
17+
-- OR
18+
RETURN TABLE(<select_query>); -- Use to return a table
19+
END;
20+
$$;
21+
```
22+
23+
## Examples
24+
25+
This example uses a loop to increment sum by iterating from -1 to 2, and the result is the sum (2):
26+
27+
```sql
28+
EXECUTE IMMEDIATE $$
29+
BEGIN
30+
LET x := -1;
31+
LET sum := 0;
32+
FOR x IN x TO x + 3 DO
33+
sum := sum + x;
34+
END FOR;
35+
RETURN sum;
36+
END;
37+
$$;
38+
39+
┌────────┐
40+
│ Result │
41+
│ String │
42+
├────────┤
43+
2
44+
└────────┘
45+
```
46+
47+
The following example returns a table with a column `1 + 1` and the value 2:
48+
49+
```sql
50+
EXECUTE IMMEDIATE $$
51+
BEGIN
52+
LET x := 1;
53+
RETURN TABLE(SELECT :x + 1);
54+
END;
55+
$$;
56+
57+
┌───────────┐
58+
│ Result │
59+
│ String │
60+
├───────────┤
61+
│ ┌───────┐ │
62+
│ │ 1 + 1 │ │
63+
│ │ UInt8 │ │
64+
│ ├───────┤ │
65+
│ │ 2 │ │
66+
│ └───────┘ │
67+
└───────────┘
68+
```

0 commit comments

Comments
 (0)