MySQL stored procedure syntax pain - mysql

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 ;

Related

How can I use the loop in MySQL?

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 ;

mysql stored procedure declaration throws error on execution

I"m continiously receiving this error when trying to create this stored procedure. I'm trying to write a procedure that splits a comma delimited string. Similar to explode. I feel I'm close.
This is 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 'DECLARE start_pos, end_pos INT;
SET start_pos = 1;
SET end_pos = Locat' at line 6
I copied the logic from a SQL Server example and did my best translate it to MySql syntax.
This is the entire procedure from start to finish. I'm hoping a well trained eye can explain why I'm getting an error.
DELIMITER $$
CREATE procedure split_string (in p_string_to_split VARCHAR(255),in p_delimiter CHAR(1) )
BEGIN
DROP TEMPORARY TABLE IF EXISTS split_channel_ids;
CREATE TEMPORARY TABLE split_channel_ids (p_channel_id int);
DECLARE start_pos, end_pos INT;
SET start_pos = 1;
SET end_pos = Locate(p_delimiter, p_string_to_split);
WHILE (start_pos < CHAR_LENGTH(p_string_to_split) + 1) DO
IF (end_pos = 0) THEN
SET end_pos = CHAR_LENGTH(p_string_to_split) + 1;
END IF;
--- INSERT split_channel_ids (p_channel_id)
--- VALUES(SUBSTRING(p_string_to_split, start_pos, end_pos - start_pos)) ;
SET start_pos = end_pos + 1;
SET end_pos = Locate(p_delimiter, p_string_to_split, start_pos);
END WHILE;
-- select * from imob_users;
select * from split_channel_ids;
END $$
DELIMITER ;
DECLARE statements (in MySQL) must be at the beginning of their enclosing BEGIN...END block.
In MS SQL, they can be anywhere; but it is annoying there because they do not have block scope, they have procedure scope, so you can't "re-use" names in independent blocks.

Trouble with Triggers #DBMS

Hi I have been asked to create a Trigger that increments a variable #count on each Insertion.
I have written this query
DELIMITER//
BEGIN
DECLARE #count int[DEFAULT NULL]
CREATE TRIGGER trig
AFTER INSERT ON students
FOR EACH ROW SET #count = #count + 1;
END//
Now at this point I get the error 1064 (42000): You have an error in your SQL syntax and so on....
In my opinion maybe there is some problem in the declare statement but that is the prescribed syntax on net.
I am using MySQL 5.6 Command Line
The syntax is not correct and its not mysql way of declare, it should be as below and you do not need to declare the session variables like you did
delimiter //
CREATE TRIGGER trig AFTER INSERT ON student
for each row
begin
set #count = #count + 1 ;
end;//
delimiter ;

MySQL Delcare causing error

I am trying to create a trigger that marks items as deleted when they are inserted into the database.
Sadly I can't get my DECLARE to stop erroring, I have looked at the DECLARE docs and also at a few examples but I must be missing something.
The query I have so far is:
CREATE TRIGGER set_deleted BEFORE INSERT ON customercontact
FOR EACH ROW
BEGIN
DECLARE numrow INT; /* line 4 */
SELECT COUNT(*)
INTO numrow
FROM orders
WHERE NEW.order_id = 1;
if numrow >= 1 THEN
SET new.deleted = 1;
END IF;
END
The error message is showing:
#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 4
Thanks for your help and preventing me from defenestrating myself!
Try this:
DELIMITER $$
CREATE TRIGGER set_deleted BEFORE INSERT ON customercontact
FOR EACH ROW
BEGIN
DECLARE numrow INT; /* line 4 */
SELECT COUNT(*)
INTO numrow
FROM orders
WHERE NEW.order_id = 1;
if numrow >= 1 THEN
SET new.deleted = 1;
END IF;
END$$
DELIMITER ;
You need to change the delimiter when you create TRIGGER or STORED PROCEDURE.
By default, MySQL itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause MySQL to pass the entire stored program definition to the server. Otherwise, MySQL breaks CREATE TRIGGER, before it reaches the END statement (on the first semicolon, which, in your case, is DECLARE statement).
You can see the documentation for more details:
http://dev.mysql.com/doc/refman/5.5/en/stored-programs-defining.html

MySQL Stored Procedure error

I have a simple stored procedure which inserts records into four character fields in table. Below is the procedure
CREATE PROCEDURE dowhile()
BEGIN
DECLARE I INT DEFAULT 5
v1loop: WHILE I < 10000 DO
INSERT INTO TestTable1(A,B,C,D)
SELECT CONCAT(I,'A'), CONCAT(I,'B'), CONCAT(I,'C'), CONCAT(I,'D')
SET I = I + 1
END WHILE v1loop
END;
Checked online - there are no free MSSQL to MYSQL SQL Conversion Tools
Error is
- SQL Syntax Error in Insert - SELECT Statement
I have checked the syntax this seem to be correct.
Any pointers for this would be helpful.
Not to bad actually, you just need to add some semi-colons and change MySQL's default delimiter. This needs to be done since we're using SQL inside SQL.
DELIMITER $$
CREATE PROCEDURE dowhile()
BEGIN
DECLARE I INT DEFAULT 5;
v1loop: WHILE I < 10000 DO
INSERT INTO TestTable1(A,B,C,D)
SELECT CONCAT(I,'A'), CONCAT(I,'B'), CONCAT(I,'C'), CONCAT(I,'D');
SET I = I + 1;
END WHILE v1loop;
END$$
DELIMITER ;
Just tested this on my MySQL server.