mysql while 语法错误,在MySQL查询中使用WHILE LOOP时出错

I'm trying to run a SQL query in MySQL (MariaDB 5.5.39)

CREATE PROCEDURE dowhile()

BEGIN

DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO

SET v1 = v1 - 1;

END WHILE;

END;

But when I run that query I get the following error(s)

MariaDB [elis27rel]> source /home/vagrant/test.sql;

--------------

CREATE PROCEDURE dowhile()

BEGIN

DECLARE v1 INT DEFAULT 5

--------------

ERROR 1064 (42000) at line 1 in file: '/home/vagrant/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near

'' at line 3

--------------

WHILE v1 > 0 DO

SET v1 = v1 - 1

--------------

ERROR 1064 (42000) at line 5 in file: '/home/vagrant/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near

'WHILE v1 > 0 DO

SET v1 = v1 - 1' at line 1

--------------

END WHILE

--------------

ERROR 1064 (42000) at line 7 in file: '/home/vagrant/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near

'END WHILE' at line 1

--------------

END

--------------

ERROR 1064 (42000) at line 8 in file: '/home/vagrant/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near

'END' at line 1

I don't know what the issues with my code are. I copied and pasted that code from the mysql website http://dev.mysql.com/doc/refman/5.0/en/while.html and was hoping to use that script to get my feet wet. Any help would be appreciated.

解决方案

I think this is what you need:

DELIMITER $$

CREATE PROCEDURE dowhile()

BEGIN

DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO

SET v1 = v1 - 1;

END WHILE;

END;

DELIMITER ;

The delimiter tells MySQL not to end the statement at the semi-colon -- which would be a problem because the stored procedure definition isn't finished at the first semicolon.

### 回答1: 可以使用MySQL的LOOP命令来实现循环操作。LOOP命令需要配合BEGIN和END语句一起使用,语法如下: ``` LOOP BEGIN -- 循环体 END; END LOOP; ``` 在循环可以使用IF、WHILE、REPEAT等语句来实现不同的循环条件和操作。需要注意的是,循环必须包含退出循环的条件,否则会导致死循环。 例如,以下代码实现了从1到10的累加操作: ``` DECLARE i INT DEFAULT 1; DECLARE sum INT DEFAULT ; LOOP BEGIN SET sum = sum + i; SET i = i + 1; IF i > 10 THEN LEAVE; END IF; END; END LOOP; SELECT sum; ``` 执行以上代码后,会输出55,即1到10的累加和。 ### 回答2: MySQL并没有单独的"loop"命令,但可以通过使用循环结构来达到类似的效果。在MySQL循环通常使用存储过程和游标来实现。 存储过程是一组预定义的SQL语句集合,可以被重复调用。它通常以BEGIN和END两个关键字包围,间包含一系列的SQL语句。使用存储过程可以实现循环操作。 游标是对查询结果集的引用,它可以被用于遍历和操作结果集的数据。通过游标,可以在循环体内访问并处理查询结果。 以下是一个示例,展示了如何在MySQL模拟循环操作: DELIMITER $$ CREATE PROCEDURE loop_example() BEGIN DECLARE i INT DEFAULT 0; DECLARE max_count INT DEFAULT 10; WHILE i < max_count DO -- 在这里编写循环的操作逻辑 -- 使用i进行计数,执行相应的SQL语句 SET i = i + 1; END WHILE; END $$ DELIMITER ; 执行以上代码后,可以通过调用存储过程"loop_example()"来执行循环操作。在循环体内,可以执行需要重复的SQL语句,比如插入数据、更新数据等。 循环的终止条件可以通过设置一个计数器变量,比如上述示例的"max_count"。循环体内使用i进行计数,并在每次迭代结束后递增计数器。 总之,虽然MySQL没有单独的"loop"命令,但可以通过结合存储过程和游标的方式实现循环操作。通过存储过程定义循环体,再通过游标操作结果集,来模拟循环的效果。 ### 回答3: MySQL并没有名为"loop"的特定命令,但可以通过其他方法来实现循环操作。 1. WHILE循环:可以使用WHILE语句实现循环。例如,要打印1到10的数字,可以使用如下代码: ``` DECLARE counter INT DEFAULT 1; WHILE counter <= 10 DO SELECT counter; SET counter = counter + 1; END WHILE; ``` 2. REPEAT循环:可以使用REPEAT语句实现循环。以下是一个打印1到10数字的示例: ``` DECLARE counter INT DEFAULT 1; REPEAT SELECT counter; SET counter = counter + 1; UNTIL counter > 10 END REPEAT; ``` 3. FOR循环MySQL并不直接支持FOR循环,但可以使用WHILE循环和控制变量来模拟。下面是一个打印1到10数字的示例: ``` DECLARE counter INT DEFAULT 1; WHILE counter <= 10 DO SELECT counter; SET counter = counter + 1; END WHILE; ``` 需要注意的是,在使用循环,务必要设置适当的循环条件和循环退出条件,以避免出现无限循环的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值