MySQL updating data in a column using a loop - mysql

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;

Related

Why won't MYSQL procedure properly iterate new ROWS in TABLE using WHILE statement?

I am creating this basic procedure using MySQL Workbench to accept a single input parameter.
The table "unique_days" has a single PRIMARY KEY column called "dayid" which currently has a single ROW with a value of 1.
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET #maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
DELETE FROM unique_days WHERE dayid > 1;
WHILE (maxdate_current > maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
The procedure is then called with an integer parameter.
CALL dayid_iteration(11);
The variables are setting properly because I can run a select statement with the variable and it shows the correct new value. The deletion of dayid > 1 also works (Tested by manually adding additional rows, and then running procedure). However, I can't seem to get the WHILE statement to insert new rows with the value provided.
Any help is much appreciated. I searched multiple other questions, and countless forums, but everything looks like it should be working.
I am expecting the code to CREATE 9 ROWS for a total of 10 ROWS.
The following is included just so you can see the starting values of the table.
SELECT * FROM unique_days;
For anyone who finds this question, the following code functions correctly. The input variable on the parameter was not setting properly. Once the parameter had "IN" placed in front of the variable name, it correctly received the parameter.
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
WHILE (maxdate_current <= maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
CALL dayid_iteration(1000);
This call procedure now properly works.
CALL dayid_iteration(1000);

bulk update with store procedure using while loop

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.

How do i get column values of my sql table dynamically using stored procedures?

I am learning stored procedures. Coming to the problem I have a table where columns are named as Q1,Q2,Q3...,Q10 . I want to retrieve these values dynamically using while loop (or for loop). I tried something but that didn't give me actual output. Please give me some suggestions.
My code is as follows :
delimiter //
drop procedure if exists exam//
create procedure exam ()
begin
declare a int default 1;
while a <= 9 do
select concat(Q,a) from feed1 where col = 'XYZ';
set a= a + 1;
end while;
end ;
//
delimiter ;
call exam;
When i run this i am getting an error " Unknown column Q in field list "

while loop terminating after fetching some data

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 ;

Can I run a loop in MySQL without using a procedure/function?

For testing, is it possible to run a loop from MySQL workbench or similar tool? I tried but got an error.
If it is possible, please supply a simple example I can run.
You can't do a for loop in an SQL editor without a stored procedure. I use TOAD for MySQL.
A quick stored procedure should do the job:
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_loop_test$$
CREATE PROCEDURE proc_loop_test()
BEGIN
DECLARE int_val INT DEFAULT 0;
test_loop : LOOP
IF (int_val = 10) THEN
LEAVE test_loop;
END IF;
SET int_val = int_val +1;
SELECT int_val;
END LOOP;
END$$
DELIMITER ;
There's a trick with limited use-cases that is "loop-like".
I wanted to create a large (1~2 million) row table of random integers for a test:
INSERT INTO test_table (num) VALUES(ROUND(RAND() * 1E6));
-- calling this will insert once for every row in test_table
INSERT INTO test_table (num)
SELECT ROUND(RAND() * 1E6)
FROM test_table;
So I quickly just kept doubling the number of rows until I had what I needed.
Supposed that you already have an arbitrary table myOldTable which is sufficiently long you could use the following trick:
set #counter = 0;
select (#counter := #counter+1), #counter*#counter from myOldTable limit 1000;
If it is that you only want to block the current thread then use select sleep(seconds); otherwise you can use a stored procedure (if there's something you want to loop over) or a UDF (user defined function).