MySql 定時任務和儲存過程,每月建立一張表

2021-09-12 12:10:17 字數 3624 閱讀 2547

1. 要做這個之前先要:

-- 設定好時區

set time_zone = '+8:00';

-- 開啟事件排程器

set global event_scheduler = 1;

-- 選擇資料庫

use test;12

3456

2. 先建立乙個儲存過程或者函式:

-- 建立owner_info_month表的儲存過程

create procedure create_owner_info_table_every_month()

begin

set @datestr = date_format(now(), '%y_%m');

set @nowdatestr = date_format(date_sub(now(), interval 1 month),'%y_%m');

set @createstr = concat("create table owner_info_" ,@datestr,

"(`owner_id` varchar(32) not null default '業戶編號必須唯一',

`owner_name` varchar(100) default null comment '業戶名稱',

`business_id` varchar(100) default null comment '經營許可證編號',

`owner_tel` varchar(30) default null comment '聯絡**',

`address` varchar(200) default null comment '所在位址',

`create_time` datetime default null comment '記錄日期',

primary key (`owner_id`),

unique key ");

set @keystr = concat(" pk_gov_owner_info_" ,@datestr,"(`owner_id`)) engine=innodb default charset=utf8;");

set @ownerinfo = concat(@createstr ,@keystr);

prepare stmt from @ownerinfo;

execute stmt;

-- 將上個月的表資料匯入到新建立的表中;

set @tablename = concat("owner_info_" ,@datestr);

set @insertstr = concat("insert into ",

@tablename,

"(owner_id, owner_name,business_id, owner_tel,address,create_time,) 

select owner_id, owner_name, business_id,owner_tel, address,create_time 

from owner_info_" ,@nowdatestr);

prepare stmt from @insertstr;

execute stmt;

end ;12

3456

78910

1112

1314

1516

1718

1920

2122

2324

2526

2728

2930

3132

3334

3536

3738

3940

4142

43建立的第二個儲存過程:

-- 建立vehicle_info_month表的儲存過程

create procedure create_vehicle_info_table_every_month ()

begin

set @datestr = date_format(now(), '%y_%m');

set @nowdatestr = date_format(date_sub(now(), interval 1 month),'%y_%m');

set @createstr = concat("create table vehicle_info_",@datestr,

"(`vehicle_id` varchar(50) not null comment '車輛唯一標識',

`vehicle_name` varchar(30) not null comment '車牌號',

`color` smallint(5) not null default '2' comment '車牌顏色',

`owner_id` varchar(32) default null,

`vehicle_type` varchar(10) default null comment '車輛型別類別編碼',

`create_time` datetime default null comment '記錄日期',

primary key (`vehicle_id`),");

set @keystr1 = concat(" key idx_**_oid_",@datestr,"(`owner_id`),");

set @keystr2 = concat(" key idx_**_trans_type_",@datestr,"(`vehicle_type`),");

set @keystr3 = concat(" key idx_**_trans_vname_",@datestr,"(`vehicle_name`)) engine=innodb default charset=utf8;");

set @vehicleinfo = concat(@createstr ,@keystr1 ,@keystr2 ,@keystr3);

prepare stmt from @vehicleinfo;

execute stmt;

-- 將上個月的表資料匯入到新建立的表中;

set @tablename = concat("vehicle_info_" ,@datestr);

set @insertstr = concat("insert into ",

@tablename,

"(vehicle_id, vehicle_name, color, owner_id,vehicle_type,create_time) 

select vehicle_id, vehicle_name, color, owner_id,vehicle_type,create_time

from vehicle_info_" , @nowdatestr);

prepare stmt from @insertstr;

execute stmt;

end;12

3456

78910

1112

1314

1516

1718

1920

2122

2324

2526

2728

2930

3132

3334

3536

3738

3940

4142

4344

3. 建立定時任務或者叫排程器事件:

mysql 儲存過程 定時任務

建立儲存過程 create procedure if not exists test begin insert into test.test time values curdate end set global event scheduler 0 設定任務器 開關 1開 0關 show variab...

mysql定時任務與儲存過程例項

檢視event是否開啟 show variables like event sche 將事件計畫開啟 set global event scheduler 1 將事件計畫關閉 set global event scheduler 0 關閉事件任務 alter event eventname on c...

MySQL 建立儲存過程及定時任務

建立定時任務名為 event csdn 2019 01 01 00 00 00 起每10s 執行 create event if not exists event csdn on schedule every 10 second starts timestamp 2019 01 01 00 00 0...