In the MySQL database, use the cursor to cyclically traverse _ MySQL

Source: Internet
Author: User
In the MySQL database, use the cursor to traverse cyclically /*
Delete all data tables in the * dt database with empty docuemttype and MD, PD, and ET documenttype:
Delete from table name where length (documenttype) <2 or documenttype is null or documenttype in ('ET ', 'MD', 'PD ');
*/
DELIMITER $
USE 'database name 1' $
Drop procedure if exists 'stored PROCEDURE name 1' $
Create procedure 'stored PROCEDURE name 1 '()
BEGIN
DECLARE str varchar (40 );
DECLARE Done int default 0;
DECLARE rs cursor for select table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'database name 1 ';
/* Exception handling */
Declare continue handler for sqlstate '000000' SET Done = 1;
/* Open the cursor */
OPEN rs;
/* Retrieve the values of the userId field of the current record one by one */
Fetch next from rs INTO str;
/* Traverse the data table */
REPEAT
Delete from str where length (documenttype) <2 or documenttype is null or documenttype in ('ET ', 'MD', 'PD ');
Fetch next from rs INTO str;
UNTIL Done end repeat;
/* Close the cursor */
CLOSE rs;
END $
DELIMITER;

Use database name 1;
Call stored procedure name 1;

/*
Delete data whose docuemttype is not empty from all data tables in * sd database
Delete from table name where length (documenttype)> 1;
*/
DELIMITER $
USE 'database name 2' $
Drop procedure if exists 'stored PROCEDURE name 2' $
Create procedure 'stored PROCEDURE name 2 '()
BEGIN
DECLARE str varchar (40 );
DECLARE Done int default 0;
DECLARE rs cursor for select table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'database name 2 ';
/* Exception handling */
Declare continue handler for sqlstate '000000' SET Done = 1;
/* Open the cursor */
OPEN rs;
/* Retrieve the values of the userId field of the current record one by one */
Fetch next from rs INTO str;
/* Traverse the data table */
REPEAT
Delete from str where length (documenttype)> 1;
Fetch next from rs INTO str;
UNTIL Done end repeat;
/* Close the cursor */
CLOSE rs;
END $
DELIMITER;

Use database name 2;
Call stored procedure name 2;

/*
Delete Non-dissertation data from all data tables in * et database:
Delete from table name where length (doucmenttype) = ''or documenttype is null or documenttype not in ('ET ', 'MD', 'PD ');
*/

DELIMITER $
USE 'database name 3' $
Drop procedure if exists 'stored PROCEDURE name 3' $
Create procedure 'stored PROCEDURE name 3 '()
BEGIN
DECLARE str varchar (40 );
DECLARE Done int default 0;
DECLARE rs cursor for select table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'database name 3 ';
/* Exception handling */
Declare continue handler for sqlstate '000000' SET Done = 1;
/* Open the cursor */
OPEN rs;
/* Retrieve the values of the userId field of the current record one by one */
Fetch next from rs INTO str;
/* Traverse the data table */
REPEAT
Delete from str where length (doucmenttype) = ''or documenttype is null or documenttype not in ('ET ', 'MD', 'PD ');
Fetch next from rs INTO str;
UNTIL Done end repeat;
/* Close the cursor */
CLOSE rs;
END $
DELIMITER;

Use database name 3;
Call stored procedure name 3;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.