-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforeach-stmt.html
More file actions
72 lines (70 loc) · 3.65 KB
/
foreach-stmt.html
File metadata and controls
72 lines (70 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>FOREACH Statement - SQL Notebook</title>
<link rel="stylesheet" href="sqlnotebook.css">
</head>
<body>
<header>
<table class="nav">
<tr>
<td>
<a href="index.html"><img src="art/SqlNotebookIcon.png" alt="SQL Notebook (logo)" style="width: 58px; height: 58px; float: left; margin-right: 20px;"></a>
</td>
<td>
<a href="index.html" id="title">SQL Notebook</a><br>
<nav>
<ul class="nav">
<li><a href="https://github.com/brianluft/sqlnotebook/releases">Download</a></li>
<li><a href="doc.html"><span id="header-doc-long">Documentation</span><span id="header-doc-short">Docs</span></a></li>
<li><a href="https://github.com/brianluft/sqlnotebook">GitHub</a></li>
</ul>
</nav>
</td>
</tr>
</table>
<hr style="margin-top: 15px; margin-bottom: 15px;">
</header>
<article><div id="article">
<h1><code>FOREACH</code> Statement</h1>Iterates over each row in a table or table expression, assigning the column
values to the specified variables and executing the provided statements for each row.<br>
<br>
The loop may be terminated early by using the <a href="break-stmt.html"><code>BREAK</code></a> statement. The current
iteration of the loop can be abandoned and the next iteration of the loop started by using the <a href=
"continue-stmt.html"><code>CONTINUE</code></a> statement.
<h2>Syntax</h2><img src="art/foreach-stmt-syntax.svg" alt="" class="railroad" moz-do-not-send="true" width="628"><br>
<h2>Parameters</h2>
<ul class="args">
<li><i>variable-name</i>: variable name<br>
The name of a variable to receive column values from each row. The variable name must begin with an at sign
(<code>@</code>), dollar sign (<code>$</code>), or colon (<code>:</code>). Variables are automatically declared if
they do not exist; the <code>FOREACH</code> statement acts as a variable declaration if needed. Multiple variables
can be specified, separated by commas.</li>
<li><i>table-name</i>: table name or expression<br>
The name of a table, view, or table expression to iterate over. Each row from this table will be processed in
order.</li>
<li><i>statement</i>: statement<br>
The statements to execute for each row of the table. If more than one statement is desired, the
<code>BEGIN</code> and <code>END</code> keywords must be used.<br></li>
</ul>
<h2>Remarks</h2>
<ul>
<li>Variables are assigned values in the order they appear in the variable list, corresponding to the table's
columns.</li>
<li>If fewer variables are specified than there are columns in the table, the extra columns are ignored.</li>
<li>If more variables are specified than there are columns in the table, the extra variables are set to NULL.</li>
<li>Variables are automatically declared if they do not already exist.</li>
<li>The table expression can be a table name, view name, or any valid SQL table expression.</li>
</ul>
<h2>Examples</h2>
<pre><i>-- Iterate over a simple table with multiple columns</i><br>DROP TABLE IF EXISTS employees;<br>CREATE TEMP TABLE employees (id INTEGER, name TEXT, salary INTEGER);<br>INSERT INTO employees VALUES (1, 'Alice', 50000);<br>INSERT INTO employees VALUES (2, 'Bob', 60000);<br><br>FOREACH (@id, @name, @salary) IN employees BEGIN<br> PRINT CONCAT(@name, ' (ID: ', @id, ') earns $', @salary);<br>END</pre>
<ul class="examples"></ul>
</div></article>
<footer><div id="footer">
<hr>
© 2016-2025 <a href="https://github.com/electroly">Brian Luft</a>
</div></footer>
</body>
</html>