0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Posted at

はじめに

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をファンクションの先頭にまとめて書いたらできた。
そういう感じなんだ、勉強になった。。。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?