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.
Related
I created a little program to show my students how to use loops in MySQL (Workbench).
The program updates a column in a table with test data. I want to add a range of interest rates. When I run the program, the same interested rate is entered into each row.
CREATE TABLE mtgPayment
(lengthMonths DECIMAL(4,0),
mInterest DECIMAL(8,6),
loanAmt DECIMAL(10,2));
DELIMITER $$
CREATE PROCEDURE test_data()
BEGIN
DECLARE i DECIMAL(8,6) DEFAULT 2.0;
WHILE i < 3.5 DO
UPDATE mtgpayment set mINTEREST = i;
SET i = i + .5;
END WHILE;
END$$
DELIMITER ;
CALL test_data();
SELECT * FROM mtgpayment;
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 am using a procedure to loop through a table and perform a different procedure on each row (this procedure uses other tables and works fine, returns correct result set). Here is my attempt:
DELIMITER $$
CREATE PROCEDURE user_loop()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM users INTO n;
SET i=0;
WHILE i<n DO
SET #cur_id = (SELECT user_id from users LIMIT i,1);
INSERT INTO results CALL other_proc(#cur_id);
SET i = i + 1;
END WHILE;
End;
$$
Not sure what the correct syntax is in order to do this, I looked but couldn't find any examples.
I have written a procedure that creates a temporary table and executes a query by fetching the rows from the temporary table.I have around 13486 rows in the temporary table.But when i am calling the procedure i observed that the procedure is getting terminated after fetching 107 rows from the temporary table.Moreover i also observed that this value is not constant..Sometimes it is 107 the other time it is 114 and some other time it is just 100.Why this happens?Please need help?Somebody please..Here is my procedure.And i came to know that while loop will terminate for >1000 iterations.Please suggest me a method to overcome this.
DELIMITER $$
DROP PROCEDURE IF EXISTS `lookup`.`test` $$
CREATE PROCEDURE `lookup`.`test` ()
BEGIN
CREATE TEMPORARY TABLE lookup.airportname(id int AUTO_INCREMENT,PRIMARY KEY(id))
AS (select distinct airport_id from lookup.airport);
SET #num=0;
SET #arpt=NULL;
SELECT count(*) INTO #num FROM airportname;
SET #i=0;
while #i<#num do
SELECT airport_id INTO #arpt FROM airportname WHERE id=#i;
select #arpt,#i;
set #i=#i+1;
end while;
END $$
DELIMITER ;
I am using mysql query browser.Thank you.
If you want to insert value into id Then it should not be auto_increment. - Remove auto_increment from table definition, it should work
delimiter |
create procedure employee_select()
Begin
Declare empno,done int(9);
Declare emp_select Cursor for select emp_no from gross_salary ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
Open emp_select; // cursor opening
read_loop: loop // start looping all the datas one by one
fetch emp_select into empno; // fetching the select value into variable empno
//note :variable name should not be same as columne name in select statement"
IF done THEN
LEAVE read_loop; // if no more rows, this makes it to leave the loop"
END IF;
//Enter the code you want do for each row
end loop;
close emp_select;
End |
delimiter ;
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 ;