简体   繁体   English

如何从MySQL触发器调用StoredProcedure或函数?

[英]how to call StoredProcedure or Functions from a MySQL Trigger?

I am working on MySQL 5.1.3 and using PHPMyAdmin 3.1.3.1 to access it. 我正在使用MySQL 5.1.3并使用PHPMyAdmin 3.1.3.1对其进行访问。 With PHP as the Server side scripting Language. 用PHP作为服务器端脚本语言。 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. 我的问题语句是,我们可以从Trigger语句中调用存储过程或函数,以便在调用INSERT | UPDATE | DELETE触发器时,它根据定义的逻辑调用SP更新其他表。

Look here Mysql Trigger Syntax 在这里查看Mysql触发器语法

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. 分别定义存储过程,然后使用简单的CALL语句从触发器调用存储过程会更容易。 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. 触发器不能使用CALL语句来调用将数据返回到客户端或使用动态SQL的存储过程。 (Stored procedures are permitted to return data to the trigger through OUT or INOUT parameters.) (允许存储过程通过OUTINOUT参数将数据返回到触发器。)

The trigger cannot use statements that explicitly or implicitly begin or end a transaction such as START TRANSACTION , COMMIT , or ROLLBACK . 触发器不能使用显式或隐式开始或结束事务的语句,例如START TRANSACTIONCOMMITROLLBACK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM