mysql - 在 MySQL 中,我可以在存储过程之外使用过程 SQL 吗?

标签 mysql stored-procedures

在 Sybase ASE 和 Microsoft SQL Server 中,您可以在 one-off SQL statement batches 中使用过程 SQL(控制流语句,如 IF/ELSE 和 WHILE、声明和设置词法变量等)。 ,像这样:

-- from https://msdn.microsoft.com/en-us/library/ms182587.aspx
DECLARE @Number INTEGER;
SET @Number = 50;
IF @Number > 100
  SELECT 'The number is large.' AS large;
ELSE 
BEGIN
  IF @Number < 10
    SELECT 'The number is small.' AS small;
  ELSE
    SELECT 'The number is medium.' AS medium;
END;

您可以将此代码直接发送到 SQL Server,无需准备它或将其放入存储过程中,SQL Server 将发回一个包含单个元组和列的表,其值为“数字中等”。

据我所知,在 MySQL 中,过程 SQL 代码仅限于出现在存储过程定义中(CREATE PROCEDURECREATE FUNCTION 语句):

mysql> delimiter //
mysql> if 32 = 32 then
    ->   select 'yes';
    -> else
    ->   select 'no';
    -> end if;
    -> //
ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near
'if 32 = 32 then
  select 'yes';
else
  select 'no';
end if'
at line 1

这个印象正确吗?

最佳答案

是的,你是对的。许多构造仅在存储函数内有效,例如 if。它甚至在 manual 中也这么说。 。

"The IF statement for stored programs implements a basic conditional construct."

但是,使用另一种方法可以实现相同的结果,即 function if

select if(32 = 32, 'yes', 'no');

sqlfiddle

关于mysql - 在 MySQL 中,我可以在存储过程之外使用过程 SQL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641447/

相关文章:

php - Mysqli 输出 10 个随机行

php - fatal error : Cannot pass parameter 2 by reference when using PHP and MYSQL

sql-server - 'CREATE/ALTER PROCEDURE' 必须是查询批处理中的第一条语句

java - 将数据记录到文件服务器上的文件还是更好地使用 MySQL?

mysql - 我如何在 MYSQL 中执行 levenstein 相似性算法?

使用 JDBC 添加相同日期后,MySql 给我错误的日期

java - 在 voltdb 的存储过程中运行选择查询后出现错误

c# - WinForms和SQL Server,将存储过程添加到database.mdf?

sql - 从SQL存储过程中提取毫秒数据

sql - 如何在存储过程中生成新的Guid?