|
| 1 | +--- |
| 2 | +layout: global |
| 3 | +title: CLOSE statement |
| 4 | +displayTitle: CLOSE statement |
| 5 | +license: | |
| 6 | + Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + contributor license agreements. See the NOTICE file distributed with |
| 8 | + this work for additional information regarding copyright ownership. |
| 9 | + The ASF licenses this file to You under the Apache License, Version 2.0 |
| 10 | + (the "License"); you may not use this file except in compliance with |
| 11 | + the License. You may obtain a copy of the License at |
| 12 | +
|
| 13 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | + Unless required by applicable law or agreed to in writing, software |
| 16 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + See the License for the specific language governing permissions and |
| 19 | + limitations under the License. |
| 20 | +--- |
| 21 | + |
| 22 | +Closes an open cursor and releases its resources. |
| 23 | + |
| 24 | +The `CLOSE` statement closes a cursor that was previously opened with `OPEN`, freeing the memory and resources associated with its result set. After closing, the cursor can be reopened with `OPEN` to execute the query again with fresh parameter bindings. |
| 25 | + |
| 26 | +## Syntax |
| 27 | + |
| 28 | +``` |
| 29 | +CLOSE cursor_name |
| 30 | +``` |
| 31 | + |
| 32 | +## Parameters |
| 33 | + |
| 34 | +- **`cursor_name`** |
| 35 | + |
| 36 | + The name of an open cursor. The cursor can be optionally qualified with a compound statement label (e.g., `outer_label.my_cursor`). |
| 37 | + |
| 38 | +## Examples |
| 39 | + |
| 40 | +```SQL |
| 41 | +-- Basic cursor lifecycle |
| 42 | +> BEGIN |
| 43 | + DECLARE x INT; |
| 44 | + DECLARE my_cursor CURSOR FOR SELECT id FROM range(3); |
| 45 | + |
| 46 | + OPEN my_cursor; |
| 47 | + FETCH my_cursor INTO x; |
| 48 | + VALUES (x); |
| 49 | + CLOSE my_cursor; |
| 50 | + END; |
| 51 | +0 |
| 52 | + |
| 53 | +-- Close cursor in handler |
| 54 | +> BEGIN |
| 55 | + DECLARE x INT; |
| 56 | + DECLARE my_cursor CURSOR FOR SELECT id FROM range(2); |
| 57 | + |
| 58 | + DECLARE EXIT HANDLER FOR NOT FOUND |
| 59 | + BEGIN |
| 60 | + CLOSE my_cursor; |
| 61 | + VALUES ('Cursor closed on completion'); |
| 62 | + END; |
| 63 | + |
| 64 | + OPEN my_cursor; |
| 65 | + REPEAT |
| 66 | + FETCH my_cursor INTO x; |
| 67 | + UNTIL false END REPEAT; |
| 68 | + END; |
| 69 | +Cursor closed on completion |
| 70 | + |
| 71 | +-- Reopen cursor with different parameters |
| 72 | +> BEGIN |
| 73 | + DECLARE x INT; |
| 74 | + DECLARE param_cursor CURSOR FOR SELECT id FROM range(10) WHERE id = ?; |
| 75 | + |
| 76 | + OPEN param_cursor USING 3; |
| 77 | + FETCH param_cursor INTO x; |
| 78 | + VALUES ('First open:', x); |
| 79 | + CLOSE param_cursor; |
| 80 | + |
| 81 | + OPEN param_cursor USING 7; |
| 82 | + FETCH param_cursor INTO x; |
| 83 | + VALUES ('Second open:', x); |
| 84 | + CLOSE param_cursor; |
| 85 | + END; |
| 86 | +First open:|3 |
| 87 | +Second open:|7 |
| 88 | + |
| 89 | +-- Qualified cursor name with label |
| 90 | +> BEGIN |
| 91 | + outer_lbl: BEGIN |
| 92 | + DECLARE outer_cur CURSOR FOR SELECT id FROM range(3); |
| 93 | + DECLARE x INT; |
| 94 | + |
| 95 | + OPEN outer_cur; |
| 96 | + FETCH outer_cur INTO x; |
| 97 | + |
| 98 | + inner_lbl: BEGIN |
| 99 | + FETCH outer_lbl.outer_cur INTO x; |
| 100 | + END; |
| 101 | + |
| 102 | + CLOSE outer_lbl.outer_cur; |
| 103 | + VALUES ('Closed from outer scope'); |
| 104 | + END; |
| 105 | + END; |
| 106 | +Closed from outer scope |
| 107 | + |
| 108 | +-- Processing all rows before close |
| 109 | +> BEGIN |
| 110 | + DECLARE x INT; |
| 111 | + DECLARE done BOOLEAN DEFAULT false; |
| 112 | + DECLARE results STRING DEFAULT ''; |
| 113 | + DECLARE my_cursor CURSOR FOR SELECT id FROM range(5); |
| 114 | + |
| 115 | + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true; |
| 116 | + |
| 117 | + OPEN my_cursor; |
| 118 | + REPEAT |
| 119 | + FETCH my_cursor INTO x; |
| 120 | + IF NOT done THEN |
| 121 | + SET results = results || CAST(x AS STRING) || ','; |
| 122 | + END IF; |
| 123 | + UNTIL done END REPEAT; |
| 124 | + CLOSE my_cursor; |
| 125 | + |
| 126 | + VALUES (results); |
| 127 | + END; |
| 128 | +0,1,2,3,4, |
| 129 | +``` |
| 130 | + |
| 131 | +## Notes |
| 132 | + |
| 133 | +- The cursor must be open before calling `CLOSE`. Attempting to close a cursor that is not open raises a `CURSOR_NOT_OPEN` error. |
| 134 | +- After closing, the cursor can be reopened with `OPEN`. This is useful when you want to re-execute the cursor's query with different parameter bindings. |
| 135 | +- Cursors are automatically closed in the following scenarios: |
| 136 | + - When the compound statement that declares them exits normally |
| 137 | + - When an `EXIT` handler is triggered (all cursors in the compound statement and nested compounds are closed) |
| 138 | + - When the compound statement exits due to an unhandled exception |
| 139 | +- It is good practice to explicitly close cursors when they are no longer needed, rather than relying on implicit closure. This frees resources earlier and makes the code's intent clearer. |
| 140 | +- Closing a cursor does not affect the cursor declaration. The cursor name remains in scope and can be reopened. |
| 141 | + |
| 142 | +## Related articles |
| 143 | + |
| 144 | +- [Compound Statement](../control-flow/compound-stmt.html) |
| 145 | +- [OPEN Statement](../control-flow/open-stmt.html) |
| 146 | +- [FETCH Statement](../control-flow/fetch-stmt.html) |
| 147 | +- [SQL Scripting](../sql-ref-scripting.html) |
0 commit comments