how to call StoredProcedure or Functions from a MySQL Trigger? - mysql

I am working on MySQL 5.1.3 and using PHPMyAdmin 3.1.3.1 to access it. With PHP as the Server side scripting Language. My problem statement is can we call a Stored Procedure or Function from the Trigger statement so that when ever an INSERT|UPDATE|DELETE trigger is called, it calls the SP for updating some other tables according to the logic defined.

Look here Mysql Trigger Syntax
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
-> FOR EACH ROW
-> BEGIN
-> IF NEW.amount < 0 THEN
-> SET NEW.amount = 0;
-> ELSEIF NEW.amount > 100 THEN
-> SET NEW.amount = 100;
-> END IF;
-> END;//
mysql> delimiter;
It can be easier to define a stored procedure separately and then invoke it from the trigger using a simple CALL statement. This is also advantageous if you want to invoke the same routine from within several triggers.
There are some limitations on what can appear in statements that a trigger executes when activated:
The trigger cannot use the CALL statement to invoke stored procedures that return data to the client or that use dynamic SQL. (Stored procedures are permitted to return data to the trigger through OUT or INOUT parameters.)
The trigger cannot use statements that explicitly or implicitly begin or end a transaction such as START TRANSACTION, COMMIT, or ROLLBACK.

Related

Create MySQL Trigger manually using VB.NET Application (Systax Error)

I'm new with MySQL Triggers.
I would like to create MySQL Triggers programmatically using my VB.NET Application so first I create a query using MySQL Workbench test it and it was a success but when I copied the query to my VB.NET Application and put it on string variable and tried to execute it. the error pops up that I have a Syntax error.
DELIMITER $$
CREATE TRIGGER `updateProductPrice`
BEFORE UPDATE ON `list_events`
FOR EACH ROW
BEGIN
IF NEW.caption <> OLD.caption
THEN
SET NEW.caption = 1;
END IF ;
END$$
DELIMITER ;
As P.Salmon said don't use DELIMITER
Use only
CREATE TRIGGER `updateProductPrice`
BEFORE UPDATE ON `list_events`
FOR EACH ROW
BEGIN
IF NEW.caption <> OLD.caption
THEN
SET NEW.caption = 1;
END IF ;
END
as content for your string variable.

mysql: use a procedure inside a trigger

I need to call a procedure that modifies root password inside a trigger.
This is the procedure
delimiter //
create procedure foobar ()
begin SET PASSWORD FOR 'root'#'localhost' = 'foobar';
end//
delimiter ;
and this is the trigger
delimiter //
create trigger rootpass
after delete on map
for each row
begin
call foobar;
end //
delimiter ;
It seems the syntax is correct but when I launch a delete I get this error
ERROR 1445 (HY000): Not allowed to set autocommit from a stored function or trigger
So I think I'm trying to do a forbidden thing, is there any way this could work in mysql?
I know the whole thing sounds weird/horrible but this is the way for my application to understand that something has been deleted.
When executing a statement would cause an implicit commit of the current transaction, that statement is not permitted within a trigger or stored function.
This includes a statement in a stored procedure that is invoked by a trigger or function -- there is no layer of abstraction here that makes the invalid action any more valid.

What is the MySQL equivalent of SQL Server's UPDATE() trigger function?

I need help converting this MS SQL update trigger to MySQL. My problem is with converting the MS SQL UPDATE() function, which identifies the specific columns that were updated:
ALTER TRIGGER [dbo].[tran_upd_action] ON [dbo].[tran_action]
FOR UPDATE AS
BEGIN
IF ##ROWCOUNT>0
BEGIN
IF update(column1) OR update(column2)
BEGIN
INSERT into tran_indexerqueue
(trankey, trkey2, tranname) SELECT tran_actionid, 0, 'tranaction' from inserted
END
END
END
This does not have the rowcount check, but the basic trigger syntax will be like this. Note that only the Super User role can create a trigger. Also, you have to change the delimiter first, which is usually a big gotcha for MSSQL DBAs.
DELIMITER $$
CREATE TRIGGER `tran_upd_action` AFTER UPDATE ON `tran_action` FOR EACH ROW BEGIN
INSERT INTO tran_indexerqueue
(trankey, trkey2, tranname)
VALUES(OLD.trankey, OLD.trkey2, OLD.tranname);
END $$
DELIMITER ;

PostgreSQL functions and triggers

I am trying out functions and triggers int postgreSQL, however i am having a problem, when the function is triggered it is giving me an error
ERROR: control reached end of trigger procedure without RETURN
this particular procedure is only executing an insert into command so i do not see why it needs a return
this is the script:
CREATE OR REPLACE FUNCTION forest_aud_func() returns trigger as $tree_stamp$
BEGIN
insert into Audit values('k',124,'l');
END;
$tree_stamp$
LANGUAGE plpgsql;
create trigger forest_aud_ins after insert on forest
for each row execute procedure forest_aud_func()
insert into forest values('Blue',1600,'Malta','Health Ltd')
The error message tells you all. You need to do a RETURN from the trigger function:
CREATE OR REPLACE FUNCTION forest_aud_func() returns trigger as $tree_stamp$
BEGIN
insert into Audit values('k',124,'l');
return new;
END;
$tree_stamp$
LANGUAGE plpgsql;
From the manual:
A trigger function must return either NULL or a record/row value having exactly the structure of the table the trigger was fired for.

Does MySQL permit callbacks in C such that when a change happens, I can be notified?

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.