为什么分隔符与mysql中的存储过程一起使用? [英] Why delimiter used with stored procedure in mysql?

查看:132
本文介绍了为什么分隔符与mysql中的存储过程一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解,为什么在MySQL的存储过程中使用定界符? 但是我做不到.

I am trying to understand, why delimiter used with stored procedure in mysql? but i couldn't.

DELIMITER //
CREATE PROCEDURE GetAllProducts()
   BEGIN
       SELECT *  FROM products;
   END //
DELIMITER ;`

推荐答案

MySQL的默认分隔符为;,用于命令行中的一个语句,类似

Mysql's default delimiter is ; which is used for one statement in the command line , something as

select * from users ;

在编写触发器或存储过程以执行整个代码时,mysql需要了解它是代码/查询块.

When you write a trigger or stored procedure to execute the entire code mysql needs to understand that its a block of code/query.

如果未提供定界符,则当mysql在存储过程或触发器中遇到任何;时,它将认为这是一条语句,并将尝试执行它.因此,我们需要为存储过程或触发器提供一个定界符,并使mysql理解该定界符中的所有内容都是一组完整的代码.

If no delimiter is provided then when mysql encounters any ; inside the store procedure or trigger it will think that as one statement and will try to execute it. So we need to provide a delimiter for store procedure or trigger and make mysql understand that anything within that delimiter is one complete set of code.

所以在您的示例中

SELECT * FROM products; 

当开头提供的分隔符不是;时,它将成为完整语句的一部分.

it will be a part of the complete statement when there is a delimiter other than ; is provided at the beginning.

这篇关于为什么分隔符与mysql中的存储过程一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆