【发布时间】:2018-09-09 14:37:29
【问题描述】:
我现在尝试用增量值填充 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 */;
【问题讨论】:
-
缺失;在 insert 之后,缺少 end if;可能未设置分隔符。
-
改变了你的建议 P.Salmon 在同一行仍然有相同的错误消息。
-
使用调用 p();在末尾。这基本上是缺失的一点
-
第一个问题是
DELIMITER ;。
标签: mysql if-statement stored-procedures while-loop mariadb