sqlite 循环语句
157浏览 · 2023-09-02 15:09:01
SQLite 支持[循环](https://geek.csdn.net/educolumn/4558e7eba19b30f3f44bd4c49596a8a6?spm=1055.2569.3001.10083)语句,包括 WHILE [循环](https://geek.csdn.net/educolumn/4558e7eba19b30f3f44bd4c49596a8a6?spm=1055.2569.3001.10083)和 FOR [循环](https://geek.csdn.net/educolumn/4558e7eba19b30f3f44bd4c49596a8a6?spm=1055.2569.3001.10083)。
下面是一个 WHILE [循环](https://geek.csdn.net/educolumn/4558e7eba19b30f3f44bd4c49596a8a6?spm=1055.2569.3001.10083)的示例:
```sql
-- 创建一个测试表
CREATE TABLE test_table (
id INTEGER PRIMARY KEY,
name TEXT
-- 插入一些[测试数据](https://geek.csdn.net/educolumn/0ae403dfc3a2481ebc47f05903364887?spm=1055.2569.3001.10083)
INSERT INTO test_table (name) VALUES ('Alice');
INSERT INTO test_table (name) VALUES ('Bob');
INSERT INTO test_table (name) VALUES ('Charlie');
-- 定义变量
DECLARE @counter INTEGER = 0;
-- WHILE 循环
WHILE @counter < 3 DO
-- 获取名称
SELECT name FROM test_table WHERE id = @counter + 1;
-- 增加计数器
SET @counter = @counter + 1;
END WHILE;