Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@nobuhito

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

巷で大流行している「ズンドコキヨシ」を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行が選択されました。

パパやったね!!!

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
3
Help us understand the problem. What are the problem?