-- 查询自定义函数的源代码
SELECT
    text
FROM
    user_source
WHERE
    name = '函数名(全大写，如：HOS_GET_SORTABLE_REF)'
    AND type = 'FUNCTION'
ORDER BY
    line;

-- 定义函数
FUNCTION hos_get_sortable_ref(
    bh IN VARCHAR2,
    len IN NUMBER DEFAULT 4
) RETURN VARCHAR2 IS v_res VARCHAR2(999);

BEGIN
SELECT
    LISTAGG(str || num, '') WITHIN GROUP (
        ORDER BY
            lev
    ) INTO v_res
FROM
    (
        SELECT
            REGEXP_SUBSTR(ele, '\D*', 1) AS str,
            LPAD(REGEXP_SUBSTR(ele, '\d+', 1), len, '0') AS num,
            lev
        FROM
            (
                SELECT
                    REGEXP_SUBSTR(bh, '\D*\d+', 1, LEVEL) AS ele,
                    LEVEL AS lev
                FROM
                    dual CONNECT BY REGEXP_SUBSTR(bh, '\D*\d+', 1, LEVEL) IS NOT NULL
            )
    );

RETURN v_res;

EXCEPTION
WHEN NO_DATA_FOUND THEN RETURN NULL;

WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);

RETURN NULL;

END hos_get_sortable_ref;