MySQL uses stored procedures to clear all table data from the database, saving data structures

Source: Internet
Author: User
Tags prepare

BEGIN
DECLARE strclear VARCHAR (256);
DECLARE done INT DEFAULT 0;
#定义游标
DECLARE curone CURSOR for select concat (' TRUNCATE TABLE ', table_name, '; ') from INFORMATION_SCHEMA. TABLES where table_schema= ' test ';
#绑定控制变量到游标, the end of the cursor loop is automatically converted to true
DECLARE CONTINUE HANDLER for don't FOUND SET done = 1;
#打开游标
OPEN Curone;
REPEAT
FETCH Curone into Strclear;
#SELECT strclear;
#动态执行SQL语句
SET @mysql = strclear;
PREPARE stmt from @mysql;
EXECUTE stmt;
Deallocate PREPARE stmt;

UNTIL done = 1 END REPEAT;

#关闭游标
CLOSE Curone;
END

MySQL uses stored procedures to clear all table data from the database, saving data structures

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.