MySQL While Loop to fillup Table具有增量值,其中Auto-Increment不可选

时间:2018-03-30 14:35:24

标签: mysql if-statement stored-procedures while-loop mariadb

我尝试用增量值填充MySQL表。后来有可能会改变为什么AUTO-INCREMENT在这里没有解决方案。 我正在使用phpMyAdmin的SWL控制台

只有settingsOperationMixerId启用了AUTO-INCREMENT

我试着做一个循环:

DELIMITER ;
CREATE PROCEDURE fill()
BEGIN
    DECLARE v1 INT DEFAULT 1;
    DECLARE b INT DEFAULT 1; #counting upwards
    DECLARE c INT DEFAULT 1; #only 1 mixer
    DECLARE d INT DEFAULT 1; #product from 1-6


     WHILE v1 < 64 DO


        INSERT INTO `settingsoperationmixer`(`settingsOperationMixerId`,
        `settingsOperationId`, `mixerId`, `productId`) VALUES (NULL,b,c,d);

        IF d < 6 THEN SET d = d + 1;
        ELSE SET d = 1;
        END IF;

        SET b = b + 1;
        SET v1 = v1 + 1;
    END WHILE;
END;
DELIMITER ;

这应该循环63次,而settingsOperationId将是1-63增量,productId应该从1-6开始,然后从1再次重新开始

并收到此错误消息:

Error

SQL query:

CREATE PROCEDURE fill()
BEGIN
DECLARE v1 INT DEFAULT 1

MySQL said: Documentation
#1064 - 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

我的表的SQL-CODE有助于解决问题:

-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 30, 2018 at 05:13 PM
-- Server version: 10.1.30-MariaDB
-- PHP Version: 7.2.2

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `scm`
--

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

--
-- Table structure for table `settingsoperationmixer`
--

CREATE TABLE `settingsoperationmixer` (
  `settingsOperationMixerId` int(11) NOT NULL,
  `settingsOperationId` int(11) NOT NULL,
  `mixerId` int(11) NOT NULL,
  `productId` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `settingsoperationmixer`
--
ALTER TABLE `settingsoperationmixer`
  ADD PRIMARY KEY (`settingsOperationMixerId`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `settingsoperationmixer`
--
ALTER TABLE `settingsoperationmixer`
  MODIFY `settingsOperationMixerId` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

2 个答案:

答案 0 :(得分:1)

使用更正

=COUNTIF(CDATE(Table1[Date Closed]),">=" &TODAY() - 14)

如果您想允许auto_increment应用,请不要将其包含在插入列表中

drop procedure if exists p;
delimiter $$
CREATE PROCEDURE p()
BEGIN DECLARE v1 INT DEFAULT 1; 
DECLARE a INT DEFAULT 1; #autoIncrement 
DECLARE b INT DEFAULT 1; #counting upwards 
DECLARE c INT DEFAULT 1; #only 1 mixer 
DECLARE d INT DEFAULT 1; #product from 1-6

 WHILE v1 < 64 DO
    INSERT INTO `settingsoperationmixer`(`settingsOperationMixerId`,`settingsOperationId`, `mixerId`, `productId`)  VALUES (a,b,c,d);
    IF d < 6 THEN 
        SET d = d + 1;
     ELSE 
        SET d = 1;
     end if;

    SET b = b + 1;
    SET v1 = v1 + 1;
END WHILE;
END $$
delimiter ;

答案 1 :(得分:1)

mariadb中的一个班轮,使用Sequence storage engine

插入t(a,b,c),从seq_0_to_63中选择seq%6 + 1,seq + 1,1;