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 1 year has passed since last update.

pgsql 関数の定義とNULL入力の挙動

0
Last updated at Posted at 2021-11-07

新しい関数を定義する

CREATE FUNCTION関数を使用する。

  • 2つの値を引数にとり足し算を返す関数を定義した。
  • RETURNS NULL ON NULL INPUTに関係なく1つでもnullを受け取れば仕様上nullを返す。
DROP FUNCTION IF EXISTS add_sample_1107(integer, integer);
CREATE FUNCTION add_sample_1107(integer, integer) RETURNS integer
    AS 'select $1 + $2;'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT
    ;

select * from add_sample_1107(1,200);
select * from add_sample_1107(null,1000);
select * from add_sample_1107(null,null);
  • RETURNS NULL ON NULL INPUTをうまく使ってみる。次の関数は2つの引数をとるが、演算をしないので返り値の方がIS NOT NULLならば通常値が返ってくる。
  • 以下のように2つ目の引数はNULLでもちゃんと1が返ってくる。

image.png

  • ここでRETURNS NULL ON NULL INPUTオプションを設定すると以下のように一番最後の一引数がNULLになってしまうとNULLでない返り値がNULLになる。

image.png

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?