在 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 PROCEDURE
或 CREATE 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');
关于mysql - 在 MySQL 中,我可以在存储过程之外使用过程 SQL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641447/