DELIMITER $$
CREATE PROCEDURE repeat()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= 100) DO
INSERT INTO VISITS VALUES ("C9YAoq", "2022-05-03 00:00:00");
SET i=i+1;
END WHILE;
END;
DELIMITER ;
The error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$
CREATE PROCEDURE repeat()
BEGIN
DECLARE i INT DEFAULT 1;
' at line 1
I'm trying to insert 100 rows inside the table using the loop but that does not work.
REPEAT is a reserved word in MySQL. If you want to use it for userland names, you should quote it or rather use another name.
Use $$ delimiter to properly mark the end of CREATE PROCEDURE statement.
The result:
DELIMITER $$
CREATE PROCEDURE `repeat`()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= 100) DO
INSERT INTO VISITS VALUES ("C9YAoq", "2022-05-03 00:00:00");
SET i=i+1;
END WHILE;
END$$
DELIMITER ;
Related
I am trying to run a query which I first typed out like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS development $$
CREATE PROCEDURE development()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 500 DO
IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF;
SET i = i + 1;
END WHILE;
END $$
CALL development() $$
DROP PROCEDURE IF EXISTS development $$
DELIMITER ;
but then "compressed" into this:
DELIMITER $$ DROP PROCEDURE IF EXISTS development $$ CREATE PROCEDURE development() BEGIN DECLARE i INT DEFAULT 1; WHILE i <= 500 DO IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF; SET i = i + 1; END WHILE; END $$ CALL development() $$ DROP PROCEDURE IF EXISTS development $$ DELIMITER ;
on one line. The problem is that the first (multiline) code works, and does what it's supposed to do, while the other (single line) version, doesn't. It doesn't fail or throw errors, it just doesn't insert the rows like the multiline version. Why is this? More importantly, how can I make the single line version work?
Thanks in advance!
please try it this way:
DELIMITER $$
DROP PROCEDURE IF EXISTS development $$ CREATE PROCEDURE development() BEGIN DECLARE i INT DEFAULT 1; WHILE i <= 500 DO IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF; SET i = i + 1; END WHILE; END $$ CALL development() $$ DROP PROCEDURE IF EXISTS development $$
DELIMITER ;
The last line break might not be neccessary.
But I am fairly confident that, when inlining the whole code you end up setting a delimiter much longe than "$$"
I just executed the completely inlined example on a random sql server of mine and always received a positive result, even though I have not set up the related db structure.
with line breaks there are complaints about missing tables (as would be expected)
Regards
Stefan
You cannot run any SQL on the same line as delimiter. delimiter is a built in mysql client command that sets the statement seperator, not an sql interpreter. It takes one argument, read up to the first space or newline. Everything else is ignored.
delimiter $$ select now() $$
No output, because everything after $$ is thrown away.
Or illustrated by bad syntax
delimiter "$$" select; nothing order by from nowhere group , oh forget it $$
this is my trigger
CREATE TRIGGER `proximo_pago` BEFORE INSERT ON `pago`
FOR EACH ROW BEGIN
declare num_orden integer;
select max(orden) from pago where lote=NEW.lote into num_orden;
if(num_orden is NULL)
then
set NEW.orden=1;
else
set NEW.orden=num_orden+1;
end if;
END
and the ERROR
SQL query:
CREATE TRIGGER `proximo_pago` BEFORE INSERT ON `pago`
FOR EACH ROW BEGIN
declare num_orden integer;
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
I've tried to use DELIMITER but it isnĀ“t works, please help me , thanks
My answer is based only on the necessity of your question which is declaration of the variable.. if the Delimiter doesn't do the trick maybe its an error in your syntax because you didn't SET your SELECT query in a variable. try this first.
DELIMITER $$
CREATE
TRIGGER `proximo_pago` BEFORE INSERT
on `pago`
FOR EACH ROW
BEGIN
DECLARE num_orden int;
SET num_orden = select max(orden) from pago where lote=NEW.lote;
if(num_orden is NULL)
then
set NEW.orden=1;
else
set NEW.orden=num_orden+1;
end if;
END$$
DELIMITER ;
I would like to execute a loop in phpmyadmin which inserts rows in a table. So far I have:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows()
With this, I get an error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER$$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE ins' at line 1
Syntax for the DROP PROCEDURE statement is incorrect!
Change
DROP PROCEDURE IF EXISTS insert_my_rows()
to
DROP PROCEDURE IF EXISTS insert_my_rows;
You need to end the statement with the proper delimiter. Change END to END$$.
You must change the delimiter only while you make blocks of statements, so during the procedure definition. The DROP PROCEDURE and CALL statements needs delimiters too.
DROP PROCEDURE IF EXISTS insert_my_rows;
DELIMITER $$
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows();
DROP PROCEDURE IF EXISTS insert_my_rows;
When running my query I am getting this:
error:
#1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use near '' at line 3
SQL:
DROP PROCEDURE IF EXISTS insertPromos;
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END;
CALL insertPromos();
You need to define a new delimiter
delimiter |
DROP PROCEDURE IF EXISTS insertPromos |
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END
|
delimiter ;
CALL insertPromos();
Otherwise the procedure definition would end at the first ; which would not be correct.
I am trying to create a simple Stored procedure that allows me to conduct mass inserts, However I am running into syntactical troubles and unable to figure out where what's going wrong, despite comparing my procedure syntax to existing examples, and it seems to be correct.
CREATE PROCEDURE populateUserTable()
BEGIN
DECLARE counter int(10);
SET counter = 1;
WHILE counter < 101 DO
INSERT INTO user(userid) values(counter);
SET counter = counter + 1
END WHILE;
END
Upon running, MYSQL states:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
and highlits this guy:
CREATE PROCEDURE populateUserTable( ) BEGIN DECLARE counter INT( 10 ) ;
What's up here?
Have you used
DELIMITER $$
At the start?
Try
DELIMITER $$
CREATE PROCEDURE populateUserTable()
BEGIN
DECLARE counter int(10);
SET counter = 1;
WHILE counter < 101 DO
INSERT INTO user(userid) values(counter);
SET counter = counter + 1
END WHILE;
END $$
DELIMITER ;