记录一个简单的Oracle存储过程,主要区别它和SQL存储过程的不同。
涉及到:输入输出参数(不能定义参数的长度)、定义变量(没有DECLARE)、变量赋值方式(SELECT INTO、:=)、字符串连接(||)、条件语句IF ELSIF END IF等。
CREATE OR REPLACE PROCEDURE SP_DELETE_BYFILENAME(V_TABLENAME NVARCHAR2, V_CONDITIONS NVARCHAR2,V_IP NVARCHAR2,V_CURRENTUSER NVARCHAR2) AS V_OUTDATEORDERCOUNT INTEGER; V_HANDLEORDERCOUNT INTEGER; V_COUNT INTEGER; V_OPERCONTENT NVARCHAR2(800); BEGIN IF V_TABLENAME='OUTDATEORDER' THEN --统计要删除的超时工单数量 SELECT COUNT(*) INTO V_OUTDATEORDERCOUNT FROM TB_BB_WORKORDERORIGINAL WHERE REMARK=V_CONDITIONS; --统计要删除的超时工单明细数量 SELECT COUNT(*) INTO V_COUNT FROM TB_BB_WORKORDERDEPARTMENT WHERE SD IN ( SELECT SD FROM TB_BB_WORKORDERORIGINAL WHERE REMARK=V_CONDITIONS ); IF V_OUTDATEORDERCOUNT > 0 OR V_COUNT > 0 THEN V_OPERCONTENT :='删除超时工单'|| V_OUTDATEORDERCOUNT||'条,删除超时工单明细'||V_COUNT||'条。'; END IF; --1:当按文件名批量删除超时工单中的数据时,也要将超时工单明细表中的数据删除 DELETE FROM TB_BB_WORKORDERDEPARTMENT WHERE SD IN ( SELECT SD FROM TB_BB_WORKORDERORIGINAL WHERE REMARK=V_CONDITIONS ); --2:按导入文件名称批量删除超时工单表中的数据 DELETE FROM TB_BB_WORKORDERORIGINAL WHERE REMARK=V_CONDITIONS; ELSIF V_TABLENAME='HANDLEORDER' THEN --统计要删除的已处理工单数量 SELECT COUNT(*) INTO V_HANDLEORDERCOUNT FROM TB_BB_WORKORDERDISPOSEORIGINAL WHERE REMARK=V_CONDITIONS; --统计要删除的超时工单明细数量 SELECT COUNT(*) INTO V_COUNT FROM TB_BB_WORKORDERDEPARTMENT WHERE ID IN ( SELECT ID FROM TB_BB_WORKORDERDISPOSEORIGINAL WHERE REMARK=V_CONDITIONS ); IF V_HANDLEORDERCOUNT > 0 OR V_COUNT > 0 THEN V_OPERCONTENT :='删除已处理工单'|| V_HANDLEORDERCOUNT||'条,删除超时工单明细'||V_COUNT||'条。'; END IF; --1:删除已处理工单数据时,也要将对应的超时工单明细表中的数据删除 DELETE FROM TB_BB_WORKORDERDEPARTMENT WHERE ID IN ( SELECT ID FROM TB_BB_WORKORDERDISPOSEORIGINAL WHERE REMARK=V_CONDITIONS ); --2:按导入文件名批量删除已处理工单中的数据 DELETE FROM TB_BB_WORKORDERDISPOSEORIGINAL WHERE REMARK=V_CONDITIONS; END IF; --3:添加操作日志 INSERT INTO TB_BB_LOG(LOGINNAME,IP,OPERTIME,OPERCONTENT)VALUES(V_CURRENTUSER,V_IP,sysdate,V_OPERCONTENT); COMMIT; END SP_DELETE_BYFILENAME;