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

发布于 2024-10-03 20:13:09 字数 156 浏览 0 评论 0原文

我正在研究 MySQL 5.1.3 并使用 PHPMyAdmin 3.1.3.1 来访问它。使用 PHP 作为服务器端脚本语言。我的问题是,我们是否可以从触发器语句中调用存储过程或函数,以便在调用 INSERT|UPDATE|DELETE 触发器时,它会调用 SP 来根据定义的逻辑更新其他一些表。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2024-10-10 20:13:09

看这里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;  

可以更容易定义单独存储过程,然后使用简单的 CALL 语句从触发器中调用它。如果您想从多个触发器中调用相同的例程,这也是有利的。

触发器在激活时执行的语句中可能出现的内容存在一些限制:

触发器不能使用 CALL 语句来调用将数据返回到客户端或使用动态 SQL 的存储过程。 (允许存储过程通过 OUTINOUT 参数将数据返回到触发器。)

触发器不能使用显式或隐式开始或结束事务的语句,例如 >START TRANSACTIONCOMMITROLLBACK

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文