28
23

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 5 years have passed since last update.

MySQLストアドプロシージャ極々サンプル

Last updated at Posted at 2013-06-20

空プロシージャ

作成

DELIMITER //

DROP PROCEDURE IF EXISTS proc1//

CREATE PROCEDURE proc1()
BEGIN
  SELECT VERSION();
END;
//
DELIMITER ; 

呼び出し方法

CALL proc1;
CALL proc1();

引数付き

CREATE PROCEDURE proc2(IN param INT)
BEGIN
  SELECT param;
END;
CALL proc2(1024);
CREATE PROCEDURE proc3(IN param1 INT, IN param2 VARCHAR(8))
BEGIN
  SELECT param1, param2;
END;
CALL proc3(1024, 'hello');

引数付き(output)

CREATE PROCEDURE proc4(OUT param1 INT, OUT param2 VARCHAR(8))
BEGIN
  SET param1 = -1;
  SET param2 = 'hello';
END;
CALL proc4(@param1, @param2);
SELECT @param1, @param2;

ファンクション

  • レプリケーションを構成する場合、DETERMINISTIC or NOT DETERMINISTICが必要となる。
  • DETERMINISTICは引数が同じであれば、返値が同じ場合、NOT DETERMINISTICは、(ランダム関数が入る等で)入力値に対して出力値が毎回違う場合に指定する。
CREATE FUNCTION func1()
RETURNS INT DETERMINISTIC
BEGIN
  RETURN -1;
END;
CREATE FUNCTION func1()
RETURNS VARCHAR(12) DETERMINISTIC
BEGIN
  RETURN 'hello, world';
END;

呼び出し方法

SELECT func1();
28
23
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
28
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?