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
Related
I need to generate a table with one field. The field will contain numbers from 0 to some n with increments of m. So, (0, m, 2m, 3m, ... n-m, n)
Thanks!
You can use recursive CTE to generate the numbers like below, 5 is increment and 100 is max value. may require minor changes for MySQL syntax, as I have written this query for SQL server.
WITh MyNumbers as(
Select 0 as Num
UNION all
SELECT Num + 5 as Num from MyNumbers where num <= 100
)
select * from MyNumbers
DELIMITER //
drop procedure if exists counter//
-- This pocedure count from 0 -> n and insert the values to the new_table
create procedure counter(IN number1 int)
begin
declare idx INT default 0;
while idx <= number1 do
insert into new_table values(idx);
set idx = idx + 1;
end while;
end//
drop procedure if exists mission//
-- This procedure reading a table with single field which contains numbers, and calling for each number to the procedure 'counter'
create procedure mission()
begin
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_num INT DEFAULT 0;
-- declare cursor for all the numbers in 'TWN' table
DEClARE num_cursor CURSOR FOR
SELECT TWN.number FROM TWN;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
-- Start the iterator
open num_cursor;
get_num: LOOP
FETCH num_cursor INTO v_num;
IF v_finished = 1 THEN
-- If the procedure have been called for all the numbers
LEAVE get_num;
END IF;
-- Call the procedure for the current number
call counter(v_num);
END LOOP get_num;
-- Close the iterator
CLOSE num_cursor;
end//
DELIMITER ;
-- Clear the table 'new_table' from values
truncate table new_table;
-- call the procedure 'mission'
call mission();
-- Display the results
select * from new_table;
I want to grab a variable (between 1-365) and use this value to create the number of empty rows in a table:
insert into tblCustomer (ID) values (), (), ();
is there an easier way to do this or is using a loop the best way?
Any help would be appreciated.
A procedure with an IN parameter is quite easy
DELIMITER $$
DROP PROCEDURE IF EXISTS test_loop$$
CREATE PROCEDURE test_loop(IN number INT)
BEGIN
DECLARE x INT(11);
SET x = 1;
WHILE x <= number DO
INSERT INTO tblCustomer(id) VALUES('');
SET x = x + 1;
END WHILE;
END$$
DELIMITER ;
How to use it
CALL test_loop(20);
I want to update a table with million of records with some optimization but i am unable to create a procedure .here is my script
DROP PROCEDURE IF EXISTS test_f;
Delimiter //
create procedure test_f()
begin
DECLARE i INT unsigned DEFAULT 1;
while i < 10 do
update top 2000 test t
set
t.total = 123
where t.total=0;
set i = i+1;
end while;
END //
how can i use top or limit in my procedure.
You can use limit with your update statement.
DROP PROCEDURE IF EXISTS test_f;
Delimiter //
create procedure test_f()
begin
DECLARE i INT unsigned DEFAULT 1;
while i < 10 do
update test t
set t.total = 123
where t.total=0
limit 2000;
set i = i+1;
end while;
end;
//
delimiter ;
Above procedure causes updating first found 2000 records only.
If you want to update records between 2000 and plus, you need to specify that in the limit clause, for example as limit 2000, 2000 which updates records with row number from 2001 to 4000.
I have a little problem. Looks like the procedure does not exist. Somehow it's dropped after the creation. I get different error each time i change something. I'm not really sure what's causing the error, maybe I'm not allowed to drop procedures and creating them in the same query.
I hope you guys can help me out.
drop procedure if exists refIntChk;
DELIMITER //
CREATE PROCEDURE refIntChk(IN district INT(11), OUT b INT(1))
BEGIN
DECLARE b INT(1);
IF district IN (select dist FROM t13)
THEN
SET b = 1;
ELSE
SET b = 0;
END IF;
END; //
DELIMITER ;
drop procedure gen if exists ;
DELIMITER //
CREATE PROCEDURE gen()
BEGIN
DECLARE rows INT(11) DEFAULT (SELECT COUNT(dist) FROM t13);
DECLARE district INT(11);
DECLARE custname VARCHAR(16);
DECLARE revenue FLOAT;
DECLARE x INT DEFAULT 10000;
DECLARE outvar INT(11);
WHILE x > 0
DO
SET district = FLOOR(RAND()*rows)+1;
CALL refIntChk(district, outvar);
IF outvar = 1
THEN
SET custname = substring(MD5(RAND()), -16);
SET revenue = (RAND() * 10);
INSERT INTO t14 VALUES(NULL, custname, district, revenue);
SET x = x - 1;
END IF;
END WHILE;
END;//
DELIMITER ;
CALL gen();
When you get errors, it's usually good to run each statement, one by one, and see which one is producing the error.
The second DROP procedure statement should be:
drop procedure if exists gen;
I am running a stored procedure. The issue seems to be that it will go into the if statement. Also for some reason or another regardless of how many selects I use it will only return the first. I've copied this from another stored procedure that works like a charm, but this one just won't go. Any ideas?
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT * FROM price_tier WHERE price_tier_id = tier_id;
SET rowCount = FOUND_ROWS();
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;
There is a bug reported related to the usage of FOUND_ROWS(). So, I recommend using Count(*) for the number of rows returned. Something like the following should work.
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT COUNT(*) INTO rowCount FROM price_tier WHERE price_tier_id = tier_id
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;