在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将发回一个包含单个元组和列的表,其值为#34;数字为中等&#34;
据我所知,在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
这种印象是否正确?
答案 0 :(得分:1)
是的,你是对的。许多构造仅在存储函数内有效,如if。它甚至在manual中也是如此。
"The IF statement for stored programs implements a basic conditional construct."
但是,使用function if
的另一种方法可以获得相同的结果select if(32 = 32, 'yes', 'no');