BeginSubrtn
Mark the beginning of a subroutine that can be entered in-line or that can be called using Gosub.
An example
12 of the 178 statements we compiled against the sample database use BeginSubrtn. This is the one with the shortest program; the rows that use BeginSubrtn are highlighted.
SELECT title, (SELECT count(*) FROM tracks t WHERE t.album_id = a.id) FROM albums a
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 22 | 0 | 0 | |
| 1 | OpenRead | 0 | 3 | 0 | 3 | 0 |
| 2 | Rewind | 0 | 21 | 0 | 0 | |
| 3 | Column | 0 | 2 | 1 | 0 | |
| 4 | BeginSubrtn | 0 | 3 | 0 | 0 | |
| 5 | Null | 0 | 4 | 4 | 0 | |
| 6 | Integer | 1 | 5 | 0 | 0 | |
| 7 | Null | 0 | 6 | 6 | 0 | |
| 8 | OpenRead | 2 | 5 | 0 | k(2,,) | 2 |
| 9 | Rowid | 0 | 7 | 0 | 0 | |
| 10 | SeekGE | 2 | 14 | 7 | 1 | 0 |
| 11 | IdxGT | 2 | 14 | 7 | 1 | 0 |
| 12 | AggStep | 0 | 0 | 6 | count(0) | 0 |
| 13 | Next | 2 | 11 | 1 | 0 | |
| 14 | AggFinal | 6 | 0 | 0 | count(0) | 0 |
| 15 | Copy | 6 | 4 | 0 | 0 | |
| 16 | DecrJumpZero | 5 | 17 | 0 | 0 | |
| 17 | Return | 3 | 5 | 1 | 0 | |
| 18 | Copy | 4 | 2 | 0 | 0 | |
| 19 | ResultRow | 1 | 2 | 0 | 0 | |
| 20 | Next | 0 | 3 | 0 | 1 | |
| 21 | Halt | 0 | 0 | 0 | 0 | |
| 22 | Transaction | 0 | 0 | 1 | 0 | 1 |
| 23 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: AggFinal, AggStep, Column, Copy, DecrJumpZero, Goto, Halt, IdxGT, Init, Integer, Next, Null, OpenRead, ResultRow, Return, Rewind, Rowid, SeekGE, Transaction.
What the SQLite documentation says
Mark the beginning of a subroutine that can be entered in-line or that can be called using Gosub. The subroutine should be terminated by an Return instruction that has a P1 operand that is the same as the P2 operand to this opcode and that has P3 set to 1. If the subroutine is entered in-line, then the Return will simply fall through. But if the subroutine is entered using Gosub, then the Return will jump back to the first instruction after the Gosub.
This routine works by loading a NULL into the P2 register. When the return address register contains a NULL, the Return instruction is a no-op that simply falls through to the next instruction (assuming that the Return opcode has a P3 value of 1). Thus if the subroutine is entered in-line, then the Return will cause in-line execution to continue. But if the subroutine is entered via Gosub, then the Return will cause a return to the address following the Gosub.
This opcode is identical to Null. It has a different name only to make the byte code easier to read and verify.
From sqlite.org's list of opcodes for SQLite 3.54.1, in the public domain. Opcodes are not a stable interface and change between releases.
← AutoCommit · BitAnd →