MYSQL声明变量 [英] MYSQL declaring variables

查看:73
本文介绍了MYSQL声明变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这个脚本有什么问题

I don't get what is wrong with this script

BEGIN
DECLARE crs INT DEFAULT 0;

WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs)
SET crs = crs + 1;
END WHILE;
END;

我希望它向表中的大陆插入10个值,但是第二行有错误.

I want it to insert 10 values into the table continent but there is an error at the second line.

推荐答案

MySQL不支持执行存储过程代码的匿名块.

MySQL does not support the execution of anonymous blocks of stored procedure code.

您需要创建一个包含该代码的存储过程,然后调用它.

You need to create a stored procedure including that code and then invoke it.

此外,您在插入语句的末尾缺少分号.我修好了.您可能还想使用concat()而不是+来生成名称,但是我将把更改留给您.

Also, you were missing the semi-colon at the end of your insert statements. I fixed that. You also probably want to use concat() instead of + to generate the names, but I'll leave that change to you.

创建过程:

DELIMITER $$

DROP PROCEDURE IF EXISTS insert_ten_rows $$

CREATE PROCEDURE insert_ten_rows () 
    BEGIN
        DECLARE crs INT DEFAULT 0;

        WHILE crs < 10 DO
            INSERT INTO `continent`(`name`) VALUES ('cont'+crs);
            SET crs = crs + 1;
        END WHILE;
    END $$

DELIMITER ;

调用过程:

CALL insert_ten_rows();

这篇关于MYSQL声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆