示例:创建独立的函数。

    1. delimiter /
    2. CREATE OR REPLACE FUNCTION f_maxid_of_t1 RETURN number
    3. IS
    4. i_id number := -1;
    5. BEGIN
    6. SELECT max(id) INTO i_id FROM t1 ;
    7. RETURN i_id;
    8. EXCEPTION
    9. WHEN OTHERS THEN
    10. RETURN NULL;
    11. END;
    12. /
    13. delimiter ;

    执行函数。

    1. obclient> select * from t1;
    2. +-------+------+---------------------+
    3. | ID | NAME | GMT_CREATE |
    4. +-------+------+---------------------+
    5. | 10000 | A | 2020-04-02 18:37:24 |
    6. | 10001 | B | 2020-04-02 18:37:41 |
    7. | 10003 | C | 2020-04-02 18:45:07 |
    8. | 10004 | D | 2020-04-02 18:45:08 |
    9. | 10005 | E | 2020-04-02 18:45:10 |
    10. +-------+------+---------------------+
    11. 5 rows in set (0.00 sec)
    12. obclient> select f_maxid_of_t1() from dual;
    13. +-----------------+
    14. | F_MAXID_OF_T1() |
    15. +-----------------+
    16. | 10005 |
    17. +-----------------+
    18. 1 row in set (0.03 sec)
    19. obclient> truncate table t1;
    20. Query OK, 0 rows affected (0.11 sec)
    21. obclient> select f_maxid_of_t1() from dual;
    22. +-----------------+
    23. | F_MAXID_OF_T1() |
    24. +-----------------+
    25. | NULL |
    26. +-----------------+
    27. 1 row in set (0.02 sec)