1. 程式人生 > >探究:MySQL是否存在像Oracle一樣的匿名塊用法?MySQL如何使用流程語句?

探究:MySQL是否存在像Oracle一樣的匿名塊用法?MySQL如何使用流程語句?

生成測試需要的大量資料的時候,需要使用到如迴圈語句這樣的流程語句?Oracle可以直接在匿名塊中使用流程語句,SQL server可以直接寫流程語句,MySQL呢?


mysql> delimiter $
mysql> set @id=1;
    -> if @id>0 then select @id;
    -> end if;
    -> $
Query OK, 0 rows affected (0.00 sec)

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 @id>0 then select @id;
end if' at line 1

必須在其他物件中使用;

mysql> create procedure testif() 
    -> begin
    -> set @id=1;
    -> if @id>0 then select @id;
    -> end if;
    -> end;
    -> $
Query OK, 0 rows affected (0.00 sec)

mysql> call testif()$
+------+
| @id  |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

也許要多做對比才能不那麼容易混淆。