MySQL使用while循环插入具有功能的多行

时间:2019-03-18 11:22:15

标签: mysql database function while-loop

我正在尝试在MySQL数据库中插入多行。首先,我为地址创建插入函数,该函数连接到仅包含格式化地址的另一个表,因此我在另一个函数中为其插入。

然后在第三个函数中,我试图用第一个为其生成伪地址填充第三个表。由于某种原因,由于SQL语法错误,即使是第一个较低级别的函数(address_insert())也无法执行,我无法弄清楚它可能是什么。有任何想法吗?

谢谢! 第一个功能:

DROP PROCEDURE IF EXISTS address_insert;
CREATE PROCEDURE address_insert()
BEGIN
DECLARE i INT DEFAULT (SELECT COUNT(*) FROM hoop.address) + 1;
DECLARE counter INT DEFAULT 0;

WHILE (counter < 5) DO 
INSERT INTO hoop.address (adr_id, address, city, country, created_at, lat, lng, updated_at, zip)
VALUES                   (i, CONCAT("Address-", i), "City", "United States", CURRENT_TIMESTAMP, RAND(35, 50), RAND(80, 120), CURRENT_TIMESTAMP, "ZIPCODE");

    SET i = i + 1;
    SET counter = counter + 1; 
END WHILE;
END;

第二个功能:

DROP PROCEDURE IF EXISTS address_shop_insert;
CREATE PROCEDURE address_shop_insert()
BEGIN
DECLARE i INT DEFAULT (SELECT COUNT(*) FROM hoop.address_shop) + 1;
DECLARE counter INT DEFAULT 0;

WHILE (counter <= SELECT COUNT(*) FROM hoop.address) DO 
INSERT INTO hoop.address_shop_insert (formatted_address, adr_id)
VALUES                               ("Formatted Address", i);

SET i = i + 1;
SET counter = counter + 1; 
END WHILE;
END;

最终功能:

DROP PROCEDURE IF EXISTS merchant_shop_insert;
CREATE PROCEDURE merchant_shop_insert()
BEGIN
address_insert()
address_shop_insert()

DECLARE i INT DEFAULT (SELECT COUNT(*) FROM hoop.merchant_shop) + 1; 
DECLARE merchant_accounts INT DEFAULT (SELECT COUNT(*) FROM hoop.merchant_account) + 1;
DECLARE shop_address_id DEFAULT (SELECT COUNT(*) FROM hoop.address_shop) - 5;
DECLARE counter INT DEFAULT 1;
DECLARE account_mod INT DEFAULT 0;
DECLARE account_id INT DEFAULT 1;

WHILE (i < merchant_accounts) DO
 IF 
    SELECT MOD(account_mod, 5) = 0
    SET account_id = account_id + 1;
 END IF;

 IF counter = 5
    SET shop_address_id = shop_address_id + 1;
    SET counter = 1;
 END IF;

  INSERT INTO hoop.merchant_shop (shop_id, contact_name, contact_phone, created_at, shop_name, status, updated_at, acc_id, shop_adr_id) 
  VALUES (i, "Strawberry Peach", "+361/789-6544", CURRENT_TIMESTAMP, CONCAT("Shop", i), 1, CURRENT_TIMESTAMP, account_id, shop_address_id)

  SET i = i + 1;
  SET account_mod = account_mod + 1;
  SET counter = counter + 1;
END WHILE;
END;

1 个答案:

答案 0 :(得分:1)

由于半列;结束了一条命令,因此您需要为存储过程的创建定义一个特定的命令,否则,MySQL将不知道什么是分隔符,该分隔符告诉了该声明的结束。程序。

DROP PROCEDURE IF EXISTS address_insert;    

-- Set the delimiter to $$
DELIMITER $$

-- Notice that every instructions IN the procedure will be ended by the regular delimiter ;
CREATE PROCEDURE address_insert()
BEGIN
    DECLARE i INT DEFAULT (SELECT COUNT(*) FROM hoop.address) + 1;
    DECLARE counter INT DEFAULT 0;

    WHILE (counter < 5) DO 
        INSERT INTO hoop.address (adr_id, address, city, country, created_at, lat, lng, updated_at, zip)
        VALUES                   (i, CONCAT("Address-", i), "City", "United States", CURRENT_TIMESTAMP, RAND(35, 50), RAND(80, 120), CURRENT_TIMESTAMP, "ZIPCODE");

        SET i = i + 1;
        SET counter = counter + 1; 
    END WHILE;
-- vv------------------------ Notice this
END$$

-- Set it back to ;
DELIMITER ;