MySQL stored procedures and events

Source: Internet
Author: User

about the stored procedures under MySQL and the creation of events

The following is the main function of the stored procedure is to query the table half a year ago data, if there is a file, and then delete the data.

CREATE definer = ' root ' @ ' localhost ' PROCEDURE ' newproc ' () beginselect COUNT (*) into the @count from S_account  where date_a DD (Registertime, Interval 6 month) <=now (); IF @count >0then Set @today =time_to_sec (now ()); Set @select_sql =concat ("select * from S_account  where Date_add (Registertime, Interval 6 month) ' <=now () into  OUTFILE ' c:/\\ ', @today, ". txt ' fields  TERMINATED by ', ';");         PREPARE Charu from @select_sql;          EXECUTE Charu; Delete from  s_account  where Date_add (Registertime, Interval 6 month) <=now (); commit; END IF; END;

fields TERMINATED by ', ' inter-field separator
Optionally enclosed by ' "' enclosing field is invalid for numeric type
LINES TERMINATED by ' \ n ' line break

The following is the code for creating the event, which starts with ' 2014-11-05 09:00:00 ' and executes the pro_test () process every day.

CREATE definer= ' root ' @ ' localhost ' EVENT ' newevent ' on SCHEDULE every 1 day starts ' 2014-11-05 09:00:00 ' on completion not PR Eserveenabledocall pro_test ();

When we create an event, it is possible to prompt the event to be turned off, which requires a manual turn-on event.

First, query the status of the scheduled event in sql: SHOW VARIABLES like ' Event_scheduler '
If the return is off, the current is off, and if it is on, the scheduled task is currently turned on.
Locate the My.ini file in the MySQL program directory and add an entry: Event_scheduler = 1
After saving, restart the MySQL service, restart the service can be found in the service management.

can also be implemented with a script:
MySQL Event_scheduler
To open the Event_scheduler SQL command:
SET GLOBAL event_scheduler = on;
SET @ @global. Event_scheduler = on;
SET GLOBAL event_scheduler = 1;
SET @ @global. Event_scheduler = 1;
Instead, close the Event_scheduler command:
SET GLOBAL event_scheduler = OFF;
SET @ @global. Event_scheduler = OFF;
SET GLOBAL event_scheduler = 0;
SET @ @global. Event_scheduler = 0;








MySQL stored procedures and events

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.