示例:创建简单的存储过程。
- 准备相关表
create table t1(id number not null primary key, name varchar2(50) not null, gmt_create date not null default sysdate, unique (name));
create sequence seq_t1 start with 10000 increment by 1 cache 50 nocycle;
- 创建存储过程
delimiter /
CREATE OR REPLACE PROCEDURE sp_insert_t1( p_name varchar2 )
AS
BEGIN
INSERT INTO t1(id, name) values(seq_t1.nextval, p_name) ;
COMMIT;
dbms_output.put_line('Add a row which name is : ' || p_name );
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
dbms_output.put_line('Exception raised!');
END ;
/
delimiter ;
- 执行存储过程
obclient> set serveroutput on ;
Query OK, 0 rows affected (0.22 sec)
obclient> select * from t1;
Empty set (0.01 sec)
obclient> call sp_insert_t1('A');
Query OK, 0 rows affected (0.05 sec)
Add a row which name is : A
obclient> select * from t1;
+-------+------+---------------------+
| ID | NAME | GMT_CREATE |
+-------+------+---------------------+
| 10000 | A | 2020-04-02 18:37:24 |
+-------+------+---------------------+
1 row in set (0.00 sec)
obclient> call sp_insert_t1('B');
Query OK, 0 rows affected (0.00 sec)
Add a row which name is : B
obclient> call sp_insert_t1('A');
Query OK, 0 rows affected (0.01 sec)
Exception raised!
obclient> select * from t1;
+-------+------+---------------------+
| ID | NAME | GMT_CREATE |
+-------+------+---------------------+
| 10000 | A | 2020-04-02 18:37:24 |
| 10001 | B | 2020-04-02 18:37:41 |
+-------+------+---------------------+
2 rows in set (0.00 sec)