需求:今天遇到mysql中的日誌表需要設定最大儲存量、最多儲存天數,多餘的刪除
儲存過程**如下:
delimiter //
drop procedure if exists log_system;
create procedure log_system (in u_n int,u_d int,s_n int,s_d int)//u_d使用者日誌天數,u_n使用者日誌數,s_n系統日誌數,s_d系統日誌天數
begin
set u_d = (select userloggingdate from logging_prameter); //通過設定表得到使用者日誌天數
delete from user_logging
where time <= (select date_add(sysdate(),interval - u_d day) as yestoday);//通過演算法算出系統時間前去使用者日誌天數,得到應該刪除的時間點
if (select count(*) from user_logging) > (select userloggingnumber from logging_prameter) then
set u_n = (select count(*) from user_logging)-(select userloggingnumber from logging_prameter);
delete from user_logging order by time limit u_n;
end if;
set s_d = (select sysloggingdate from logging_prameter);
delete from sys_logging where time <= (select date_add(sysdate(),interval - s_d day) as yestoday);
if (select count(*) from sys_logging) > (select sysloggingnumber from logging_prameter) then
set s_n = (select count(*) from sys_logging)-(select sysloggingnumber from logging_prameter);
delete from sys_logging order by time limit s_n;
end if;
end;
//delimiter
定時器**如下:
delimiter //
set global event_scheduler = on;
drop event if exists `eventsysuserlogging`;
create event if not exists `eventsysuserlogging`
on schedule
every 1 day
starts timestamp(current_date,'17:30:00')
on completion preserve enable
do
begin
call log_system(@u_n,@u_n,@s_n,@s_d);//呼叫儲存過程
end;
// delimiter
MySQL儲存過程,定時器
檢視儲存過程狀態 show procedure status 檢視儲存過程詳細內容 查詢定時器設定 show global variables like event scheduler 設定定時器 set global event scheduler 1 將定時器與儲存過程繫結 create eve...
oracle定時器呼叫儲存過程
資料庫技術學習 介紹orcale定時器的簡單用法,希望大家一看就能明白 1.建立乙個表,為了能清楚看到定時器的運 況我們建立乙個帶有日期欄位的表 sql createtablejob table run timedate 2.建立儲存過程 sql createor replaceprocedurej...
oracle定時器呼叫儲存過程
介紹orcale定時器的簡單用法,希望大家一看就能明白 1.建立乙個表,為了能清楚看到定時器的運 況我們建立乙個帶有日期欄位的表 sql create table job table run time date 2.建立儲存過程 sql create orreplace procedure job ...