Help us understand the problem. What is going on with this article?

[mariaDB] ストアドファンクション内のDECLARE文でシンタックスエラーが出る

はじめに

mariaDBでストアドファンクションを定義している最中、絶対に間違っていないDECLARE文でエラーが出ました。

定義したかったFunction
CREATE OR REPLACE FUNCTION test (arg INT)
RETURNS INT DETERMINISTIC
BEGIN
  DECLARE numA INT DEFAULT 0;
  SET numA = arg;

  DECLARE numB INT DEFAULT 0;
  SET numB = numA;

  RETURN numB;
END;

受け取った引数を変数numAに入れて、
numAを変数numBに入れて、
numBをreturnするだけ。

結果
ERROR 1064 (42000) at line 3 in file: '(ファイル名)': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DECLARE numB INT DEFAULT 0;
  SET numB = numA;

  RETURN numB;
END' at line 8

DECLARE numB INT DEFAULT 0;の辺りがおかしいよと。
え~?numAはよくてnumBはダメなの?

変数宣言は先頭だけだった

定義できたFunction
CREATE OR REPLACE FUNCTION test (arg INT)
RETURNS INT DETERMINISTIC
BEGIN
  DECLARE numA INT DEFAULT 0;
  DECLARE numB INT DEFAULT 0;

  SET numA = arg;
  SET numB = numA;

  RETURN numB;
END;
結果
Query OK, 0 rows affected (0.00 sec)

DECLAREをファンクションの先頭にまとめて書いたらできた。
そういう感じなんだ、勉強になった。。。

参考

https://www.atmarkit.co.jp/ait/articles/0209/12/news001.html

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした