I created this test procedure :
DELIMITER //
CREATE PROCEDURE `str` (IN var1 INT)
BEGIN
WHILE var1 < 5 DO
SELECT var1;
SET var1 = var1 + 1;
END WHILE;
END //
DELIMITER ;
when I run it via phpmyadmin, nothing happens. no error, no confirm message. If I CALL str(1), I get a message saying the procedure doesn't exist. What's wrong here?
If you want to get the iterated value of var1 you need the SELECT outside of the WHILE loop. The following works fine in my MySQL install:
DROP PROCEDURE IF EXISTS `str`;
DELIMITER //
CREATE PROCEDURE `str` (IN var1 INT)
BEGIN
WHILE var1 < 5 DO
SET var1 = var1 + 1;
END WHILE;
SELECT var1;
END //
DELIMITER ;
Executing CALL str(1) returns 5
Related
CREATE PROCEDURE PROCEDURENAME()
BEGIN
IF ((CONDITION),SELECT 0, SELECT 1)); //not working
IF condition THEN statement END IF; //not working
IF condition
statement //not working
END
How should I properly write the if statement structure? Anyone have a working example? Please help me.
Clearly read the last stored procedure structure on the below page
http://www.mysqltutorial.org/mysql-if-statement/
please use your code like this one or simply put your code and condition in below code so I will make it fix
DELIMITER //
CREATE PROCEDURE `proc_IF` (IN param1 INT)
BEGIN
DECLARE variable1 INT;
SET variable1 = param1 + 1;
IF variable1 = 0 THEN
SELECT variable1;
END IF;
IF param1 = 0 THEN
SELECT 'Parameter value = 0';
ELSE
SELECT 'Parameter value <> 0';
END IF;
END //
I don't get what is wrong with this script
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs)
SET crs = crs + 1;
END WHILE;
END;
I want it to insert 10 values into the table continent but there is an error at the second line.
declare variable in MySQL with # and assign with :=
SET #crs = 0; // declaration
--here your query
#crs := #crs+1 // assignment
References
user defined variables
assignment
MySQL does not support the execution of anonymous blocks of stored procedure code.
You need to create a stored procedure including that code and then invoke it.
Also, you were missing the semi-colon at the end of your insert statements. I fixed that. You also probably want to use concat() instead of + to generate the names, but I'll leave that change to you.
Create the procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_ten_rows $$
CREATE PROCEDURE insert_ten_rows ()
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs);
SET crs = crs + 1;
END WHILE;
END $$
DELIMITER ;
Invoke the procedure:
CALL insert_ten_rows();
I have the following stored procedure where I need to insert a set of data for the columns Category, Function & Status. Where category always should be '1' and Function 1 to 80, status always 'ACTIVE'.
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= 80 DO
insert into functiontocategory (category,`function`,`status`) values ('1',x,'ACTIVE');
SET x = x+1;
END WHILE;
END
But it gives me 160 rows of inserted data, where 2 sets of 1 to 80s instead of one set. what is wrong with my procedure.
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `test`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test`()
BEGIN
DECLARE X INT;
SET X = 1;
WHILE X <= 80 DO
INSERT INTO functiontocategory (category,`function`,`status`) VALUES ('1',X,'ACTIVE');
SET X = X+1;
END WHILE;
END$$
DELIMITER ;
I created a database as test and a table functiontocategory (category int(3),function text,status varchar(10)) and a procedure with a name test..for testing I used call test() and it inserted 80 rows exactly
I don't get what is wrong with this script
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs)
SET crs = crs + 1;
END WHILE;
END;
I want it to insert 10 values into the table continent but there is an error at the second line.
declare variable in MySQL with # and assign with :=
SET #crs = 0; // declaration
--here your query
#crs := #crs+1 // assignment
References
user defined variables
assignment
MySQL does not support the execution of anonymous blocks of stored procedure code.
You need to create a stored procedure including that code and then invoke it.
Also, you were missing the semi-colon at the end of your insert statements. I fixed that. You also probably want to use concat() instead of + to generate the names, but I'll leave that change to you.
Create the procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_ten_rows $$
CREATE PROCEDURE insert_ten_rows ()
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs);
SET crs = crs + 1;
END WHILE;
END $$
DELIMITER ;
Invoke the procedure:
CALL insert_ten_rows();
I am trying to create a mysql stored procedure, but I get this error:
Script line: 2 Failed to CREATE PROCEDURE proc_test_bideep
The stored procedure code is:
DELIMITER $$
DROP PROCEDURE IF EXISTS `commun`.`insert_categorie` $$
CREATE PROCEDURE `commun`.`insert_categorie` (id_mere INT,
lib_categ VARCHAR(50),
id_categ_sup INT ,
categ_authInstantBuy INT)
BEGIN
SET #bg_mere := (SELECT categ_bg FROM categ_basic WHERE categ_id = id_mere);
#bg_mere+2,categ_level_bideep,categ_statut,categ_adult,categ_authSmallBid,categ_authBid,categ_authInstantBuy);
SELECT '1' AS code_retour; END IF;
ecetera.........
END $$
DELIMITER ;
a) You need to DECLARE any variables on the first lines of the procedure, including their datatype:
DECLARE bg_mere INT;
b) To fetch a value from the database into a variable, you use SELECT ... INTO syntax:
SELECT categ_bg INTO bg_mere FROM categ_basic WHERE categ_basic.categ_id = id_mere;
c) You have an END IF without the corresponding IF.
d) The closing END needs a semicolon (not BEGIN though), only then do you need a delimiter to finish the entire statement, and finally you should reset the delimiter back to normal:
BEGIN
# body of the stored procedure goes here
END;
$$
DELIMITER ;
Your parameters are missing the keyword IN such as: ...(IN id_mere INT, IN lib_categ ...). Also, you need to configure your OUT variable for #bg_mere in the initial parameter list such as (IN xxx, ..., OUT bg_mere VARCHAR/INT/WHATEVER).