5
3

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.

Oracleの表関数でズンドコキヨシ

Last updated at Posted at 2016-03-17

巷で大流行している「ズンドコキヨシ」をOracleの表関数でキメてみた☆

CREATE OR REPLACE TYPE TYPE_ZUNDOKO AS OBJECT(
	COL VARCHAR2(256)
);
/
CREATE OR REPLACE TYPE TYPE_ZUNDOKO_TABLE AS TABLE OF TYPE_ZUNDOKO;
/
CREATE OR REPLACE FUNCTION ZUNDOKO(MAXROW NUMBER DEFAULT 100) RETURN TYPE_ZUNDOKO_TABLE PIPELINED
IS
    WORD VARCHAR(4);
	TYPE WORD_ARRAY IS VARRAY(2) OF VARCHAR2(4);
    WORD_LIST WORD_ARRAY := WORD_ARRAY('ズン', 'ドコ');
    TYPE HISTORY_ARRAY IS VARRAY(5) OF VARCHAR2(4);
    HISTORY HISTORY_ARRAY := HISTORY_ARRAY('', '', '', '', '');
BEGIN
    FOR I IN 1..MAXROW LOOP
        WORD := WORD_LIST(TRUNC(DBMS_RANDOM.VALUE() * 2 + 1));
        PIPE ROW(TYPE_ZUNDOKO(WORD));
        IF HISTORY(2) || HISTORY(3) || HISTORY(4) || HISTORY(5) || WORD = 'ズンズンズンズンドコ' THEN
			PIPE ROW(TYPE_ZUNDOKO('キ・ヨ・シ!'));
			EXIT;
		END IF;
		HISTORY(1) := HISTORY(2);
		HISTORY(2) := HISTORY(3);
		HISTORY(3) := HISTORY(4);
		HISTORY(4) := HISTORY(5);
        HISTORY(5) := WORD;
    END LOOP;
    RETURN;
END;
/

で、このテーブル関数をセレクトすると以下が表示されるはず。

SQL> SELECT * FROM TABLE(ZUNDOKO());

COL
--------------------------------------------------------------------------------
ドコ
ズン
ズン
ズン
ドコ
ズン
ズン
ズン
ズン
ズン
ズン
ズン
ズン
ズン
ドコ
キ・ヨ・シ!

16行が選択されました。

パパやったね!!!

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?