[英]Inserting random data into a table in MySql
Could someone please help me?有人可以帮我吗? I don't know what the problem here
我不知道这里有什么问题
DROP PROCEDURE IF EXISTS insertRandom;
CREATE PROCEDURE insertRandom()
BEGIN
DECLARE mytime timestamp;
SET mytime := '2009-01-01 00:00:00'
BEGIN
test_loop : LOOP
while mytime < now()
mytime = mytime + interval '8 hours';
insert into tempdata(temp_val, datum) values((select random()*(110)-10), mytime);
END LOOP;
END;
CALL insertRandom;
SELECT * FROM `temp_table`;
Looks like a semicolon in the procedure body is terminating the statement.看起来程序体中的分号正在终止该语句。 We need MySQL to see the
CREATE PROCEDURE
as a single statement, but MySQL is chopping the statement off, seeing a complete statement when it sees a semicolon.我们需要 MySQL 将
CREATE PROCEDURE
视为单个语句,但 MySQL 正在切断该语句,当它看到分号时会看到一个完整的语句。
The default statement delimiter in MySQL is semicolon ;
MySQL 中默认的语句分隔符是分号
;
character.特点。 The default delimiter can be overridden;
可以覆盖默认分隔符; we change with the
DELIMITER
statement.我们用
DELIMITER
语句改变。
Example here of temporarily modifying the statement delimiter to be two dollar sign characters $$
.此处将语句分隔符临时修改为两个美元符号字符
$$
示例。 Within the procedure body, occurrences of the semicolon character won't terminate the statement... MySQL will keep reading until it finds two dollar sign characters.在过程体中,分号字符的出现不会终止语句...... MySQL 将继续读取,直到找到两个美元符号字符。 (Goes without saying that the procedure body shouldn't contain
$$
) (不用说,程序主体不应该包含
$$
)
DELIMITER $$
DROP PROCEDURE IF EXISTS insertRandom $$
CREATE PROCEDURE insertRandom()
...
...
...
END$$
DELIMITER ;
CALL insertRandom();
Note that we use the DELIMITER
statement to change the delimiter back to the semicolon character.请注意,我们使用
DELIMITER
语句将分隔符改回分号字符。 I did not check the body of the procedure for other syntax errors;我没有检查程序主体是否存在其他语法错误; but some things stick out.
但有些东西很突出。 For example, this looks wrong
例如,这看起来不对
mytime = mytime + interval '8 hours';
that should probably be SET
statement, and the syntax is like this:那应该是
SET
语句,语法如下:
SET mytime = mytime + INTERVAL 8 HOUR;
In the SET
statement, we can use the :=
in place of =
as the assignment operator, so equivalent在
SET
语句中,我们可以使用:=
代替=
作为赋值运算符,因此等效
SET mytime := mytime + INTERVAL 8 HOUR;
For looping withing a MySQL stored program, we can do a LOOP ... END LOOP
and include a LEAVE
statement, or we can use a WHILE
loop, or ...对于 MySQL 存储程序的循环,我们可以执行
LOOP ... END LOOP
并包含一个LEAVE
语句,或者我们可以使用WHILE
循环,或者 ...
Syntax documented here:此处记录的语法:
https://dev.mysql.com/doc/refman/5.7/en/while.html https://dev.mysql.com/doc/refman/5.7/en/while.html
https://dev.mysql.com/doc/refman/5.7/en/loop.html https://dev.mysql.com/doc/refman/5.7/en/loop.html
You have many errors in your code你的代码有很多错误
Use this Procedure instead改用这个程序
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `insertRandom`()
BEGIN
DECLARE mytime timestamp;
SET mytime := '2009-01-01 00:00:00';
DROP TEMPORARY TABLE IF EXISTS tempdata;
CREATE TEMPORARY TABLE tempdata (temp_val BIGINT, datum timestamp);
test_loop : LOOP
IF mytime >= now() THEN
LEAVE test_loop;
END IF;
SET mytime = TIMESTAMPADD(HOUR,8,mytime);
insert into tempdata(temp_val, datum) values((select (rand()*110)-10), mytime);
END LOOP;
END$$
DELIMITER ;
And then进而
call insertRandom();
SELECT * FROM tempdata;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.