For loop to repeat 20 times in sql statement - mysql

I am using below but getting error, what is wrong in this code :
declare
begin
for i in 1..20
loop
execute immediate 'update table IMP_BACKUP set name='XYZ' where status='INVALID'';
end loop;
end;
/

Declare variable i as
declare
i number(2);
begin
for i in 1..20 loop
execute immediate 'update table IMP_BACKUP set name=''XYZ'' where status=''INVALID''';
end loop;
end;
/

Related

mysql table trigger throughs error

Getting error on following trigger:
DELIMITER $$
CREATE TRIGGER limit_refferals AFTER INSERT
ON wpmr_aff_referrals
FOR EACH ROW
BEGIN
DECLARE vCNT INT;
DECLARE USERID varchar(50);
DECLARE AFFILIATEID varchar(50);
DECLARE i INTEGER;
DECLARE curs1 CURSOR FOR SELECT USER_ID,affiliate_id
FROM `wpmr_aff_referrals` WHERE affiliate_id=:NEW.affiliate_id;
SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO vCNT;
IF (vCNT>=3)
set i=1;
OPEN curs1;
read_loop: LOOP
FETCH curs1 INTO USERID,AFFILIATEID;
SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO vCNT;
IF (vCNT>=3)
set i=i+1;
ELSE
set new.affiliate_id= AFFILIATEID;
END IF;
END LOOP read_loop;
CLOSE curs1;
END IF;
END$$
DELIMITER ;
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near ':NEW.affiliate_id; SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO
vCNT;
IF (vC' at line 10
DELIMITER $$
CREATE TRIGGER limit_refferals BEFORE INSERT
ON wpmr_aff_referrals
FOR EACH ROW
BEGIN
DECLARE vCNT INT;
DECLARE USERID varchar(50);
DECLARE AFFILIATEID varchar(50);
DECLARE i INTEGER;
DECLARE curs1 CURSOR FOR
SELECT USER_ID,affiliate_id
FROM `wpmr_aff_referrals` WHERE affiliate_id=NEW.affiliate_id;
SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT;
IF (vCNT >=3) THEN /*<------------Made changes at this line*/
set i=1;
OPEN curs1;
read_loop : LOOP
FETCH curs1 INTO USERID,AFFILIATEID;
SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT;
IF (vCNT>=3) THEN /*<------------Made changes at this line*/
set i=i+1;
ELSE
set new.affiliate_id= AFFILIATEID;
END IF;
END LOOP read_loop;
CLOSE curs1;
END IF;
END$$
DELIMITER ;
Try above code.
One more thing you can't use NEW row for BEFORE UPDATE TRIGGER.So i had made changes to AFTER UPDATE TRIGGER.
We don't need : for NEW and OLD value,you can directly check that value using NEW.VAL and OLD.VAL.
Also whenever you use IF IN TRIGGER,PROCEDURE and FUNCTION put THEN and then write you logic
Hope this will help you.
Remove the ":" to pass the parameter NEW.affiliate_id to the CUSTOMERLEVEL function like this:
SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT

Nested Loop not working in Mysql

I'm trying to implement a nested loop in Mysql without cursors.
DELIMITER $$
CREATE FUNCTION abc()
RETURNS INT
BEGIN DECLARE val INT;
DECLARE row,col INT DEFAULT 1;
SET val=1;
row_loop: LOOP
SET col=1;
col_loop: LOOP
SET col=col+1;
SET val=val+1;
IF col>8 THEN
LEAVE col_loop;
END IF;
END LOOP col_loop;
SET row=row+1;
IF row>8 THEN
LEAVE row_loop;
END IF;
RETURN val;
END LOOP row_loop;
END;
$$
The output should be 64 rather but I'm getting 8 as the inner loop is not running after 1st iteration.
Your logic is fine, except the return statement is inside the loop:
row_loop: LOOP
SET col=1;
col_loop: LOOP
SET col=col+1;
SET val=val+1;
IF col>8 THEN
LEAVE col_loop;
END IF;
END LOOP col_loop;
SET row=row+1;
IF row>8 THEN
LEAVE row_loop;
END IF;
END LOOP row_loop;
RETURN val;

Mysql loop errors

I have a syntax error with my code, which adds 1000 random records to a table.\
CREATE PROCEDURE addrecords()
BEGIN
DECLARE a INT Default 1;
my_loop: LOOP
<INSERTING>
SET a = a + 1;
IF a=1001 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
END
first syntax error is at default 1 saying it's missing a semicolon, then at my_loop and there are like 4 more.. Any help please? It seems good to go from what I've gooogled.
You need to change the delimiter before defining the statement:
DELIMITER $$
CREATE PROCEDURE addrecords()
BEGIN
DECLARE a INT Default 1;
my_loop: LOOP
<INSERTING>
SET a = a + 1;
IF a=1001 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
END
$$
Otherwise, the ; will end the entire CREATE PROCEDURE statement.

MySQL - Jump to "foo:Begin" in Stored Procedure

For example, I've code like this:
...
PROCEDURE procedure1(... some parameters ...)
BEGIN
DECLARE l_xxx VARCHAR(5);
SET l_xxx = 'ERROR';
IF l_xxx = 'ERROR' THEN
-- in this section, I want to call 'foo' process below
END IF;
foo:BEGIN
-- Some process
END;
END;
Can I call the error:BEGIN in a stored procedure..?
You could create another stored procedure, and call it instead of GOTO-like command.
Try to do it using IF-THEN statement with a help of user variable.
For example:
DECLARE l_xxx VARCHAR(5);
SET l_xxx = 'ERROR';
SET #a = NULL;
IF l_xxx = 'ERROR' THEN
SET #a = 1; -- Set error flag
END IF;
IF #a IS NULL THEN
-- Some process, when there were no errors.
END IF;
IF #a = 1 THEN
BEGIN
-- Some process
END;
END IF;

How to replace a string with another in whole tables in a mysql database

I want to replace a string with another in full database. Assume database name is "A" and it contains 101 tables. I want to change "subhojit" to "jeet" in all table's column if it contains "subhojit".
Is it possible in MySql?
Need to write procedure?
Please reply.
update thetable set thecol = replace(thecol, 'subhojit', 'jeet')
where thecol like '%subhojit%';
From here.
This is a bit rough and ready (exception handling for example). But hopefully you can tidy it up for your own purposes:
DELIMITER $$
CREATE PROCEDURE `replace_value_in_all_cols`(IN i_schema varchar(250),IN i_fromVal varchar(250),IN i_toVal varchar(250), OUT o_errMessage varchar(250))
BEGIN
ALL_TEXT_COLUMNS : BEGIN
DECLARE noMoreRows boolean;
DECLARE db varchar(250);
DECLARE tbl varchar(250);
DECLARE col varchar(250);
DECLARE allTextCols CURSOR FOR
select c.table_schema,c.table_name,c.column_name
from information_schema.columns c
where c.table_schema = i_schema
and lower(data_type) in ('char','text','varchar');
DECLARE EXIT HANDLER for SQLEXCEPTION set o_errMessage := "Some error message";
declare continue handler for not found set noMoreRows := true;
open allTextCols;
UPDATE_LOOP : loop
fetch allTextCols
into db,tbl,col;
if noMoreRows then
close allTextCols;
leave UPDATE_LOOP;
end if;
SET #update_stmt:=CONCAT("UPDATE ",db,".",tbl," SET ",col," = replace(",col,",'",i_fromVal,"','",i_toVal,"');");
PREPARE update_stmt FROM #update_stmt;
EXECUTE update_stmt;
DEALLOCATE PREPARE update_stmt;
end loop UPDATE_LOOP;
END ALL_TEXT_COLUMNS;
END$$
DELIMITER ;
Then you can do something like:
call `replace_value_in_all_cols`("A",'subhojit','jeet', #err);