Delete records in SQL using the Procedure with Loop - mysql

I am trying to delete the 10 million records from DB and more with DELETE procedure of SQL. I want to delete the records in loop like with each interaction record should delete 1 like below.
drop procedure if exists PROC_WEEKLY_TASK_DELETE;
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `PROC_WEEKLY_TASK_DELETE`()
BEGIN
set #rows = (select count(*) from MY_TABLE where IS_VALID_DATE='false');
set #a = 0;
while(#a<#rows)
do delete from MY_TABLE where IS_VALID_DATE='false' limit 1;
set #a = (#a+1);
commit;
end while;
END
//
DELIMITER ;
I am using above procedure to delete the record but getting problem like syntax other like
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER' at line 1
Please help me to create the procedure to delete the record in loop. Thanks

Thanks for your all suggestions specially #Akina
Please check my new Procedure which worked for me to delete the records.
drop procedure if exists PROC_WEEKLY_TASK_DELETE_LIMIT;
DELIMITER $$
CREATE DEFINER=root#localhost PROCEDURE PROC_WEEKLY_TASK_DELETE_LIMIT()
BEGIN
repeat delete from MY_TABLE where IS_VALID_DATE = 'false' limit 10000;
until not row_count()
end repeat;
END$$
DELIMITER ;
Above procedure runs successfully on local but in case of server you need to remove the DEFINER only

Related

How to create a stored proceedure in mysql?

Just trying to make a procedure that copies one table into a new one but keep getting all sorts of errors..
eg:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS ' at line 2
DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS SELECT * FROM raw_api_data
END$$
DELIMITER ;`
thanks
You have to end all commands with a semicolon or else mysql will search for more code
DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS SELECT * FROM raw_api_data;
END$$
DELIMITER ;
But if you don't have more commands
Make a simple without DELIMITER
CREATE PROCEDURE spClean_T()
CREATE TABLE staging_table AS SELECT * FROM raw_api_data;

Mysql Procedure Syntax error on Update

DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (FAT_UCI, FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD)
SELECT FAT_UCI, FAT_DESCRICAO, FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD
FROM pad_fato WHERE (FAT_ID = IDFATO);
END//
BEGIN
UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI = UCI);
END//
DELIMITER ;
Error (12,1): 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 UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI=UCI) at line 2
You have double "END" word after each query.
And not pretty formatted code :)
DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (
FAT_UCI,FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,FAT_DATA_CAD
)
SELECT
FAT_UCI,
FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,
FAT_DATA_CAD
FROM pad_fato
WHERE (FAT_ID=IDFATO);
UPDATE tad_termo_de_ajustamento
SET
TAD_STATUS_ID="2"
WHERE (TAD_FK_PRE_UCI=UCI);
END//
DELIMITER ;

SQL trigger giving error

I'm trying to call a stored procedure to create multiple rows using some of the cell values of the new row inserted in the table resource but I'm getting an error. Here is my query:
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END;
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
What am I doing wrong?
delimiter |
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END
|
delimiter ;
If you don't set another delimiter than ;, then the statement will end at the first ; and your trigger definition will be incomplete. You need to tell MySQL that the statement should end at the delimiter you defined. After that you can set the delimiter back with delimiter ;

Mysql error 1064 in Mysql 5.1- unable execute successfully

I am trying to write trigger in Mysql (5.1), but getting following error, please help.
The error is:
SQL Error (1064): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '' at line 5.
Purpose for writing trigger:
I am writing application where I am assigning users, and I want to store unassigned usercount to field cluster_count in IX_branchdetails table.After updating the base table.
trigger:
DELIMITER $$
CREATE TRIGGER upd_trg AFTER
UPDATE ON DBNAME.BASETABLE
FOR EACH ROW
BEGIN
DECLARE m_branchcode INTEGER;
DECLARE cnt INTEGER;
DECLARE cursor_branch CURSOR FOR
SELECT DISTINCT branchcode
FROM ix_branchdetails;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cursor_branch;
my_loop: loop
set done = false;
fetch cursor_branch into m_branchcode;
if done then
leave my_loop;
end if;
select count(1) into cnt from (select count(1) from BASETABLE Where IX_BRANCHCODE = m_branchcode) as temp;
update DBANAME.ix_branchdetails set DBANAME.ix_branchdetails.cluster_count = cnt where DBANAME.ix_branchdetails.BRANCHCODE = m_branchcode;
end loop my_loop;
close cursor_branch;
END $$
DELIMITER ;
I don't see a declare for the done variable:
DECLARE done TINYINT DEFAULT FALSE;
The semicolon (;) is the default delimiter for MySQL statements. To get a procedure/function/trigger defined, we normally see the statement delimiter changed to a string that doesn't appear in the statement:
DELIMITER $$
CREATE PROCEDURE ...
END$$
DELIMITER ;
If the delimiter is not changed from the semicolon, then when MySQL encounters the first semicolon in your procedure/function/trigger, it sees that as the end of the statement, which is not what you want. You want MySQL to see the entire block of code as a single statement.

mySQL stored procedure error

I am trying to use a if statement in my stored mySQL procedure, but when I try to create it in mySQL workbench I get this error ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database'.'table' WHERE date=dateIn;.
Here is the code:
DELIMITER $$
CREATE DEFINER=`rates_d_db` PROCEDURE `byDate`(in dateIn VARCHAR(255),in action VARCHAR(255))
BEGIN
IF action = "edit" THEN EDIT `database`.`table` WHERE date=dateIn;
ELSE SELECT * FROM `database`.`table` WHERE date=dateIn;
END IF;
END$$
I am new to stored procedures, so it's probably a very noob mistake.
Thanx in advance!
date is a reserved word in mySQL. You will have to wrap that in backticks as well.
Here is correct version of your procedure
DELIMITER $$
CREATE DEFINER=`rates_d_db` PROCEDURE `byDatee`(in dateIn VARCHAR(255),in action VARCHAR(255))
BEGIN
IF action = "edit" THEN
-- I used select below as i don't know what you want in edit either alter table or update table
SELECT * FROM `database`.`table` WHERE date=dateIn;
ELSE
SELECT * FROM `database`.`table` WHERE date=dateIn;
END IF;
END $$