在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将传回一个包含单个元组和列的表,其值为"The number is medium“。
据我所知,在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这种印象是正确的吗?
发布于 2015-04-15 12:15:11
是的,你是对的。很多结构只在存储函数中有效,比如if。甚至在manual中也是这样说的。
"The IF statement for stored programs implements a basic conditional construct."但是,使用另一种方法也可以获得相同的结果,即使用function if
select if(32 = 32, 'yes', 'no');https://stackoverflow.com/questions/29641447
复制相似问题