oracle 存储过程实现数据CURD操作
1.创建数据库表:
-- Create table create table TEST_TABLE ( userid NUMBER not null, username NVARCHAR2(50), userno NVARCHAR2(60), cardid NVARCHAR2(18), createdate DATE, redate DATE ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 next 8 minextents 1 maxextents unlimited ); -- Add comments to the table comment on table TEST_TABLE is '??????'; -- Add comments to the columns comment on column TEST_TABLE.userid is 'id'; comment on column TEST_TABLE.username is '用户名'; comment on column TEST_TABLE.userno is '用户编号'; comment on column TEST_TABLE.cardid is '身份证号'; comment on column TEST_TABLE.createdate is '创建日期'; comment on column TEST_TABLE.redate is '修改日期'; -- Create/Recreate primary, unique and foreign key constraints alter table TEST_TABLE add constraint USERID primary key (USERID) using index tablespace USERS pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited );
2.创建sequences
-- Create sequence create sequence S_TEST_TABLE minvalue 1 maxvalue 9999999999999999999999 start with 21 increment by 1 cache 20;
3.创建触发器triggers
CREATE OR REPLACE TRIGGER Triger_test_table BEFORE INSERT ON test_table FOR EACH ROW DECLARE INTEGRITY_ERROR EXCEPTION; ERRNO INTEGER; ERRMSG CHAR(200); DUMMY INTEGER; FOUND BOOLEAN; BEGIN -- COLUMN "userid" USES SEQUENCE S_TEST_TABLE SELECT S_TEST_TABLE.NEXTVAL INTO :NEW.userid FROM DUAL; -- ERRORS HANDLING EXCEPTION WHEN INTEGRITY_ERROR THEN RAISE_APPLICATION_ERROR(ERRNO, ERRMSG); END;
4.在包体中创建数据包名:在包中实现存储过程的实现。包头:
CREATE OR REPLACE PACKAGE SUNNY_PK IS -- AUTHOR : SUNNY -- CREATED : 2014/4/7 13:49:11 -- PURPOSE : SUNNY_PK TYPE OUTPUTLIST IS REF CURSOR; PROCEDURE SP_TEST_TABLEINSETUPDATE ( VUSERID IN TEST_TABLE.USERID%TYPE DEFAULT NULL, VUSERNAME IN TEST_TABLE.USERNAME%TYPE DEFAULT NULL, VUSERNO IN TEST_TABLE.USERNO%TYPE DEFAULT NULL, VCARDID IN TEST_TABLE.CARDID%TYPE DEFAULT NULL, VRESULT OUT INT, VERRORTEST OUT VARCHAR2, DB_OPTION_ACTION IN VARCHAR2 DEFAULT NULL ); -- AUTHOR : SUNNY -- CREATED : 2014/4/7 13:49:11 -- PURPOSE : SUNNY_PK 实现数据信息获取 PROCEDURE SP_GET_test_table ( Vuserid INT, VRETURN_LIST OUT OUTPUTLIST ); END SUNNY_PK;
包体:
CREATE OR REPLACE PACKAGE BODY SUNNY_PK IS -- AUTHOR : SUNNY -- CREATED : 2014/4/7 13:49:11 -- PURPOSE : SUNNY_PK 实现数据增删改 PROCEDURE SP_TEST_TABLEINSETUPDATE ( VUSERID IN TEST_TABLE.USERID%TYPE DEFAULT NULL, VUSERNAME IN TEST_TABLE.USERNAME%TYPE DEFAULT NULL, VUSERNO IN TEST_TABLE.USERNO%TYPE DEFAULT NULL, VCARDID IN TEST_TABLE.CARDID%TYPE DEFAULT NULL, VRESULT OUT INT, VERRORTEST OUT VARCHAR2, DB_OPTION_ACTION IN VARCHAR2 DEFAULT NULL ) IS VCOUNT INT; BEGIN IF (UPPER(DB_OPTION_ACTION)='INSERT') THEN SELECT COUNT(*) INTO VCOUNT FROM TEST_TABLE WHERE USERID = VUSERID AND (USERNO = VUSERNO OR USERNAME = VUSERNAME); IF(VCOUNT > 0) THEN VRESULT :=1; VERRORTEST := '已存在該人员信息编号或人员名稱!'; RETURN; END IF; INSERT INTO TEST_TABLE(USERNAME, USERNO ,CARDID,CREATEDATE) VALUES(VUSERNAME, VUSERNO, VCARDID, SYSDATE ); END IF; VRESULT :=0; IF(UPPER(DB_OPTION_ACTION)='UPDATE') THEN SELECT COUNT(*) INTO VCOUNT FROM TEST_TABLE WHERE USERID <> VUSERID AND USERID IN (SELECT USERID FROM TEST_TABLE WHERE USERID = VUSERID) AND (USERNO = VUSERNO OR USERNAME = VUSERNAME); IF(VCOUNT > 0) THEN VRESULT :=1; VERRORTEST := '已存在該人员信息编号或人员名稱!'; RETURN; END IF; UPDATE TEST_TABLE SET USERNO=VUSERNO, USERNAME=VUSERNAME, CARDID=VCARDID, REDATE=SYSDATE WHERE (USERID = VUSERID); VRESULT :=0; END IF; IF(UPPER(DB_OPTION_ACTION)='DELETE') THEN DELETE FROM TEST_TABLE WHERE USERID=VUSERID; VRESULT:=0; END IF; COMMIT; END; -- AUTHOR : SUNNY -- CREATED : 2014/4/7 13:49:11 -- PURPOSE : SUNNY_PK 实现数据信息获取 PROCEDURE SP_GET_test_table ( Vuserid INT, VRETURN_LIST OUT OUTPUTLIST ) IS BEGIN OPEN VRETURN_LIST FOR SELECT userid, username,userno,cardid,createdate,redate FROM test_table WHERE userid=Vuserid ; END; END SUNNY_PK;
5.根据用户编号递归实现获取部门信息
创建函数方法通过部门编号获取部门名称的函数
--通过部门编号获取部门名称的函数 create or replace function getdepartname(pdeptno in number ) return varchar2 is vdname varchar2(50); begin select dname into vdname from dept where deptno=pdeptno ; return (vdname); exception when no_data_found then -- dbms_output.put_line("没有改编号部门!"); return (null); when too_many_rows then -- dbms_output.put_line("有重复编号部门!"); return (null); when others then -- dbms_output.put_line("发生其他错误!"); return (null); end getdepartname;
存储过程:
-- AUTHOR : SUNNY -- CREATED : 2014/4/7 13:49:11 -- PURPOSE : SUNNY_PK --递归实现获取部门信息 PROCEDURE SP_GET_DEPARTMENT ( Vempno INT, VRETURN_LIST OUT OUTPUTLIST ) IS VGROUPID INTEGER DEFAULT 0; BEGIN SELECT deptno INTO VGROUPID FROM emp WHERE empno = Vempno; OPEN VRETURN_LIST FOR SELECT GETDEPARTNAME(DEPTNO) GROUPNAME,DEPTNO FROM ( SELECT DISTINCT DEPTNO FROM dept CONNECT BY PRIOR DEPTNO=PARENTID START WITH PARENTID=VGROUPID UNION SELECT VGROUPID FROM DUAL ); COMMIT; END;