Not sure what session will be use for event execution. Will mysql always create new session or just reuse old while executing event ? what happens to user-defined variable ? what happen if different events use same user-defined variable( I know it is not good to use user-defined var here, but need to know what's the result).
some example code to explain a little bit:
CREATE EVENT myevent_a
ON SCHEDULE EVERY 1 minute
DO
Set #a = ...
UPDATE myschema.mytable SET mycol = #a;
CREATE EVENT myevent_b
ON SCHEDULE EVERY 1 minute
DO
Set #a = ...
UPDATE myschema.mytable SET mycol = #a;
will #a be read/write concurrently?
Related
I didn't know anything about Stored procedures. unfortunately I have to run a stored procedure automatically.
I have a table called pass and I want to update status as cancelled at the end of the day automatically. I'm using phpmyadmin in wampserver.
I have already tried some codes but I don't know this code is correct and how to set execution automatically.
here is my code,
CREATE PROCEDURE MyTask
AS
BEGIN
SET NOCOUNT ON;
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
-- For executing the stored procedure at 1:00 A.M
declare #delayTime nvarchar(50)
set #delayTime = '01:00'
while 1 = 1
BEGIN
waitfor time #delayTime
BEGIN
ALTER PROCEDURE [dbo].[updateDuration] AS
UPDATE pass SET status = 'Cancelled' WHERE duration_type='Daily' && duration = DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY);
--Name for the stored proceduce you want to call on regular bases
execute [gatepass].[dbo].[updateDuration];
END
END
END
It says that to put below code, but I don't know where to put these.
-- Sets stored procedure for automatic execution.
sp_procoption #ProcName = 'MyTask',
#OptionName = 'startup',
#OptionValue = 'on'
Method #1 - Batching
Create a script named runMyTask.sql or whatever you want.
That script should have your SQL to run the stored procedure:
-- Sets stored procedure for automatic execution.
CALL sp_procoption('MyTask','startup', 'on');
Then you need a batch script which you might name runMyTask.bat.
In the .bat file you add the line that runs the mysql.exe command line tool and redirects the contents of the runMyTask.sql file along the lines of something like
C:/wamp/bin/mysql/mysql..../bin/mysql -u root --password=your_password database_name < C:/path/to/runMyTask.sql
When you have tested that the batch script runs correctly from the windows cmd shell, set it up in the scheduler.
Method #2 MySQL - Event Scheduler
To use the event scheduler you'll need to first make sure you it's enabled on the server. By default it is not. From the aforementioned mysql command line, login as root.
SET GLOBAL event_scheduler = ON;
After you turn it on, run a SHOW PROCESSLIST; command and you should see a line similar to this one, showing the user event_scheduler owning a process.
| 10729276 | event_scheduler | localhost | NULL | Daemon | 4 | Waiting on empty queue | NULL | 0.000 |
At this point you can add "events". To add your stored procedure run as an event, once an hour:
USE your_db;
CREATE EVENT IF NOT EXISTS EventRunMyTask
ON SCHEDULE EVERY 1 HOUR
ENABLE
DO CALL sp_procoption('MyTask','startup', 'on');
If everything goes well you should be able to see information about the scheduled job using SHOW EVENTS;
For test correctness of query I need disable all triggers in db.
I see that in information_schema exists table TRIGGERS.
Is possible temporarily disable all triggers using this table?
E.g. like:
update TRIGGERS set TRIGGERS_SCHEMA='myschema_new'
where TRIGGERS_SCHEMA='myschema'
and after finish all test return all triggers like:
update TRIGGERS set TRIGGERS_SCHEMA='myschema'
where TRIGGERS_SCHEMA='myschema_new'
May be this can corrupt db or after triggers will not works? I didn't found about it in documentation.
You can't disable triggers directly and I wouldn't recommend doing what you're suggesting but you could have your trigger check if a variable (in my example below #disable_triggers) is NULL before executing the trigger's content. For example:
Query:
SET #disable_triggers = 1;
// Your update statement goes here.
SET #disable_triggers = NULL;
Triggers:
IF #disable_triggers IS NULL THEN
// Do something use as the trigger isn't disabled.
END IF;
It is not possible to 'disable' triggers in mysql, however a trick that can be used to get around this
Add a condition in your triggers like:
if (DISABLE_TRIGER <> 1 ) then
#trigger body
end if;
and than if you want to disable triggers on import just:
SET #DISABLE_TRIGER = 1;
do imports
SET #DISABLE_TRIGER = 0;
Does MySQL permit callbacks in C such that when a change happens in the database, like an insert, that is performed by a different program or by the user at the command line, I can be notified?
I am guessing that it doesn't, because mysqlclient is a library, not a running thread. But I may as well ask.
Create a trigger like so.
DELIMITER $$
CREATE TRIGGER ad_mytable_each AFTER DELETE ON MyTable FOR EACH ROW
BEGIN
#write code that trigger After delete (hence the "ad_" prefix)
#For table MyTable (The _MyTable_ middle)
#On each row that gets inserted (_each suffix)
#
#You can see the old delete values by accesing the "old" virtual table.
INSERT INTO log VALUES (old.id, 'MyTable', old.field1, old.field2, now());
END$$
DELIMITER ;
There are triggers for INSERT, DELETE, UPDATE
And they can fire BEFORE or AFTER the action.
The trigger BEFORE the action can cancel the action by forcing an error, like so.
CREATE TRIGGER bd_mytable_each BEFORE DELETE ON MyTable FOR EACH ROW
BEGIN
#write code that trigger Before delete (hence the "db_" prefix)
declare DoError Boolean;
SET DoError = 0;
IF old.id = 1 THEN SET DoError = 1; END IF;
IF (DoError = 1) THEN SELECT * FROM Table_that_does_not_exist_to_force_error;
#seriously this example is in the manual.
END$$
DELIMITER ;
This will prevent deletion of record 1.
A before UPDATE Trigger can even change the values updated.
CREATE TRIGGER bu_mytable_each BEFORE UPDATE ON MyTable FOR EACH ROW
BEGIN
IF new.text = 'Doon sucks' THEN SET new.text = 'Doon rules';
END$$
DELIMITER ;
Hope you'll be Trigger happy.
MySQL's triggers allow you to hook into insert/update/delete queries and do something additional. You could log them in a separate table, for example.
Well you could attach a trigger to user defined function, and have it call an external program, that would then notify your code..
http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html#qandaitem-B-5-1-10
You can use triggers combined with UDFs (user defined functions) so that the corresponding action on the database executes a trigger that calls a C/C++ function.
Just consider that this mechanism runs your code inside the mysql server process, not in the client side.
I tried with this syntax but it never transferred the data.
CREATE EVENT event_test1
ON SCHEDULE EVERY 1 MINUTE STARTS '2010-09-02 15:19:25'
ON COMPLETION NOT PRESERVE ENABLE DO
insert into schema2.table1 (ID) select ID from schema1.table1 Limit 1000
I don't see the o/p in the table1 of schema2, what did i miss?
Did you turn events on?
SET GLOBAL event_scheduler = 'ON';
Also, the SHOW EVENTS command will show you if the event is there and running.
Is there a decent way to delay execution of mysql trigger?
WHILE #condition = 0
sleep for awhile
insert into some_table values(NEW.value1, NEW.value2);
Since MySQL 5.0.12, you can do this:
SELECT SLEEP(<seconds>);
The seconds parameter can be in a fraction of a second like .5.
DO SLEEP(<seconds>);
is better. There is no reason to just run SELECT statements inside triggers without needing the result set.
If you really want to do this you need to do it like here:
SET #nothing = (SELECT SLEEP(<seconds>));
But I recommend to use DO. And don't forget that a trigger is just a single statement per default. If you have more then 1 statement in your trigger you need to use BEGIN/END:
BEGIN
DO SLEEP(<seconds>);
UPDATE ...;
END