由于语法错误,无法填充表

yqlxgs2m  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(325)

我正在尝试从phpmyadmin上的数据库填充一个表,因此我有以下脚本:

USE avi; //database

SET @listOfStates = 'AK, AL, AR, AS, AZ, CA, CO, CT, DC, DE, FL, GA, GU, HI, IA, ID, IL, IN, KS, KY, LA, MA, MD, ME, MI, MN, MO, MP, MS, MT, NC, ND, NE, NH, NJ, NM, NV, NY, OH, OK, OR, PA, PR, RI, SC, SD, TN, TX, UM, UT, VA, VI, VT, WA, WI, WV, WY';

SET @item = SUBSTRING_INDEX(@listOfStates, ',', 1); 
SET @itemValue = 0;

CREATE PROCEDURE populate()
BEGIN
SET @strLen = LENGTH(@listOfStates); 

label1: LOOP

    SET @strLen = LENGTH(@listOfStates);
    SET @item = SUBSTRING_INDEX(@listOfStates, ',', 1); 
    SELECT COUNT(*) INTO @itemValue FROM us_accidents u WHERE u.state LIKE item;

    INSERT INTO state_cases (name, cases) VALUES (@item, @itemValue);

    SET @SubStrLen = LENGHT(SUBSTRING_INDEX(listOfStates, ',', 1));
    SET @listOfStates =  MID(listOfStates, SubStrLen, strLen);

    IF listOfStates = NULL
        LEAVE label1;
    ITERATE label1;

    END LOOP label1;

END;

当我尝试导入时,我得到了这个

我用set@错了吗?或者是我的程序或是varchar有什么问题?
我提到表已经创建。

4bbkushb

4bbkushb1#

你的主要问题是你错过了 DELIMITER 在存储过程开始之前。
此外,还有 IF 过程末尾的语句格式不正确:语法为 IF ... THEN ... END IF; . 最后,检查是否为空 IS NULL 而不是 = NULL .
编译成功:

SET @listOfStates = 'AK, AL, AR, AS, AZ, CA, CO, CT, DC, DE, FL, GA, GU, HI, IA, ID, IL, IN, KS, KY, LA, MA, MD, ME, MI, MN, MO, MP, MS, MT, NC, ND, NE, NH, NJ, NM, NV, NY, OH, OK, OR, PA, PR, RI, SC, SD, TN, TX, UM, UT, VA, VI, VT, WA, WI, WV, WY';

SET @item = SUBSTRING_INDEX(@listOfStates, ',', 1); 
SET @itemValue = 0;

DELIMITER //

CREATE PROCEDURE populate()
BEGIN
    SET @strLen = LENGTH(@listOfStates); 

    label1: LOOP

        SET @strLen = LENGTH(@listOfStates);
        SET @item = SUBSTRING_INDEX(@listOfStates, ',', 1); 
        SELECT COUNT(*) INTO @itemValue FROM us_accidents u WHERE u.state LIKE item;

        INSERT INTO state_cases (name, cases) VALUES (@item, @itemValue);

        SET @SubStrLen = LENGHT(SUBSTRING_INDEX(listOfStates, ',', 1));
        SET @listOfStates =  MID(listOfStates, SubStrLen, strLen);

        IF listOfStates IS NULL THEN
            LEAVE label1;
        END IF;

        ITERATE label1;

    END LOOP label1;

END; 
//

DELIMITER ;

相关问题