mysql按月自动建库并添加权限

时间:2023-02-23 20:04:21

开发有个按月自动建库并添加权限的需求。

CREATE  PROCEDURE `create_database`()
BEGIN
declare cur_date,las_date BIGINT;
declare createdb,grantdb varchar(4000);
select date_format(now(),'%Y%m%d')+20 into cur_date;
select date_format(last_day(curdate()),'%Y%m%d') into las_date;


if cur_date=las_date then
set @insql2=concat('grant all privileges on ','icosthisbak',date_format(now(),'%Y%m')+1,'.* TO `test`@`%`' );
set @insql= concat('create database ','icosthisbak',date_format(now(),'%Y%m')+1,';' );
select @insql,@insql2;
PREPARE insert_online FROM @insql;
PREPARE insert_online2 FROM @insql2;
EXECUTE insert_online;
EXECUTE insert_online2;
select createdb;
else
select '不需要创建!';
end if ;
end