1、显示使用游标(并定义返回记录类型)
DECLARE
TYPE detail_record IS RECORD
( ENCOUNTERID PRPA.ENCOUNTER.ENCOUNTERID%type,
SICKBEDID PRPA.ENCOUNTER.SICKBEDID%type,
DISPLAYNAME PRPA.ENCOUNTER.DISPLAYNAME%type,
BIRTHTIME PRPA.ENCOUNTER.BIRTHTIME%type,
SEQNOTEXT PRPA.ENCOUNTER.SEQNOTEXT%type);
TYPE detail_cursor IS REF CURSOR RETURN detail_record;
v_prodcur detail_cursor ;
v_prodrec detail_record;
BEGIN
OPEN v_prodcur FOR SELECT ENCOUNTERID, SICKBEDID, DISPLAYNAME, BIRTHTIME, SEQNOTEXT FROM PRPA.ENCOUNTER
WHERE ISDELETED=0 AND ENCOUNTERKINDCODEID=1 AND RESPONSIBLEDEPARTMENTID=4;
LOOP
FETCH v_prodcur INTO v_prodrec ;
EXIT WHEN v_prodcur%notfound;
dbms_output.put_line ('ENCOUNTERID:' || CAST(v_prodrec.ENCOUNTERID AS VARCHAR));
dbms_output.put_line ('SEQNOTEXT:' || v_prodrec.SEQNOTEXT);
END LOOP;
CLOSE v_prodcur;
END;
CREATE OR REPLACE TRIGGER REPORT.DOCTORSHIFTRECORDDETAIL_TRG
AFTER INSERT OR DELETE OR UPDATE OF ISNEWINPATIENT, ISOUTPATIENT, ISTRANSFEREDIN, ISTRANSFEREDOUT, ISDEATH, ISCRITICAL, ISSURGERY, ISLEVEL4SURGERY, ISBEDSIDE, ISSPECIAL, ISOTHER, ISDELETED
ON REPORT.DOCTORSHIFTRECORDDETAIL
FOR EACH ROW
begin
merge into REPORT.DOCTORSHIFTRECORD a
using (SELECT DOCTORSHIFTRECORDID, COUNT(1) CURRENTTOTALCOUNT FROM REPORT.DOCTORSHIFTRECORDDETAIL WHERE ISDELETED=0 AND DOCTORSHIFTRECORDID = :NEW.DOCTORSHIFTRECORDID GROUP BY DOCTORSHIFTRECORDID) b
on (a.DOCTORSHIFTRECORDID=b.DOCTORSHIFTRECORDID AND a.ISDELETED=0)
when matched then update SET a.CURRENTTOTALCOUNT=b.CURRENTTOTALCOUNT;
merge into REPORT.DOCTORSHIFTRECORD a
using (select DOCTORSHIFTRECORDID, SUM(ISNEWINPATIENT) NEWINPATIENTCOUNT, SUM(ISOUTPATIENT) OUTPATIENTCOUNT, SUM(ISTRANSFEREDIN) TRANSFEREDINCOUNT, SUM(ISTRANSFEREDOUT) TRANSFEREDOUTCOUNT
, SUM(ISDEATH) DEATHCOUNT, SUM(ISCRITICAL) CRITICALCOUNT, SUM(ISSURGERY) SURGERYCOUNT, SUM(ISLEVEL4SURGERY) LEVEL4SURGERYCOUNT, SUM(ISBEDSIDE) BEDSIDECOUNT
, SUM(ISSPECIAL) SPECIALCOUNT, SUM(ISOTHER) OTHERCOUNT FROM REPORT.DOCTORSHIFTRECORDDETAIL WHERE ISDELETED=0 AND DOCTORSHIFTRECORDID=:NEW.DOCTORSHIFTRECORDID GROUP BY DOCTORSHIFTRECORDID) b
on (a.DOCTORSHIFTRECORDID=b.DOCTORSHIFTRECORDID AND a.ISDELETED=0)
when matched then update SET a.NEWINPATIENTCOUNT=b.NEWINPATIENTCOUNT, a.OUTPATIENTCOUNT=b.OUTPATIENTCOUNT, a.TRANSFEREDINCOUNT=b.TRANSFEREDINCOUNT, a.TRANSFEREDOUTCOUNT=b.TRANSFEREDOUTCOUNT
, a.DEATHCOUNT=b.DEATHCOUNT, a.CRITICALCOUNT=b.CRITICALCOUNT, a.SURGERYCOUNT=b.SURGERYCOUNT, a.LEVEL4SURGERYCOUNT=b.LEVEL4SURGERYCOUNT, a.BEDSIDECOUNT=b.BEDSIDECOUNT
, a.SPECIALCOUNT=b.SPECIALCOUNT, a.OTHERCOUNT=b.OTHERCOUNT;
end;
2、比较两个时间(按分钟、小时、天数):
select sysdate,to_date('2023-6-5 11:35:39','yyyy-mm-dd hh24:mi:ss'), ROUND(TO_NUMBER(sysdate - to_date('2023-6-5 11:35:39','yyyy-mm-dd hh24:mi:ss')) * 24 * 60) from dual;
select sysdate,to_date('2023-6-5 11:35:39','yyyy-mm-dd hh24:mi:ss'), ROUND(TO_NUMBER(sysdate - to_date('2023-6-5 11:35:39','yyyy-mm-dd hh24:mi:ss')) * 24) from dual;
select sysdate,to_date('2022-12-9 11:35:39','yyyy-mm-dd hh24:mi:ss'), ROUND(TO_NUMBER(sysdate - to_date('2022-12-9 11:35:39','yyyy-mm-dd hh24:mi:ss'))) from dual;
3、触发器(更新某一个字段时触发):
CREATE TABLE "PORX"."COMBINEDMEDICATIONREQUESTSTATUSMOIDFIED"
("COMBINEDMEDICATIONREQUESTID" NUMBER(10,0) NOT NULL ,
"OLDSTATUSCODEID" NUMBER(5,0) DEFAULT (-1) NOT NULL ,
"NEWSTATUSCODEID" NUMBER(5,0) DEFAULT (-1) NOT NULL ,
"ROWVERSION" DATE DEFAULT (sysdate))
TABLESPACE "JXTBSPACE" ;
CREATE OR REPLACE TRIGGER "PORX"."COMBINEDMEDICATIONREQUEST_TRG"
BEFORE UPDATE OF STATUSCODEID OR DELETE ON "PORX"."COMBINEDMEDICATIONREQUEST" FOR EACH ROW
BEGIN
CASE
WHEN UPDATING ('STATUSCODEID') THEN
IF :new.STATUSCODEID != :old.STATUSCODEID THEN
INSERT INTO "PORX"."COMBINEDMEDICATIONREQUESTSTATUSMOIDFIED"(COMBINEDMEDICATIONREQUESTID, OLDSTATUSCODEID, NEWSTATUSCODEID)
VALUES(:old.COMBINEDMEDICATIONREQUESTID, :old.STATUSCODEID, :new.STATUSCODEID);
END IF;
WHEN DELETING THEN
RAISE_APPLICATION_ERROR(-20003, '不能物理删除记录');
END CASE;
END;
4、NULLIF 函数在触发器的典型应用:
CREATE OR REPLACE TRIGGER REPORT.trg_updDOCTORSHIFTRECORDDETAIL AFTER UPDATE ON REPORT.DOCTORSHIFTRECORDDETAIL FOR each row
DECLARE
v_ip varchar2(30);
v_user varchar2(30);
BEGIN
SELECT sys_context('userenv', 'ip_address'), sys_context('userenv', 'SESSION_USER') INTO v_ip, v_user FROM dual;
IF :new.ISGENERATED = 0 THEN
INSERT INTO "REPORT"."DOCTORSHIFTRECORDDETAILTRACE" (DOCTORSHIFTRECORDDETAILID, DOCTORSHIFTRECORDID, ORDINAL, ENCOUNTERID, SICKBEDID, SICKBEDNO, DISPLAYNAME, BIRTHTIME, GENDERCODEID, GENDERDESC, SEQNOTEXT
, ISNEWINPATIENT, ISOUTPATIENT, ISTRANSFEREDIN, ISTRANSFEREDOUT, ISDEATH, ISCRITICAL, ISSURGERY, ISLEVEL4SURGERY, ISBEDSIDE, ISSPECIAL, ISOTHER, ISGENERATED
, GENERATEDON, ILLNESSSTATEBRIEF, CREATEEMPLOYEEID, MODIFIEDEMPLOYEEID, ROWVERSION, ISDELETED, DIAGNOSIS, SURGERIES, NOTE, SICKBEDORDINAL
, N_DOCTORSHIFTRECORDID, N_ORDINAL, N_ENCOUNTERID, N_SICKBEDID, N_SICKBEDNO, N_DISPLAYNAME, N_BIRTHTIME, N_GENDERCODEID, N_GENDERDESC, N_SEQNOTEXT
, N_ISNEWINPATIENT, N_ISOUTPATIENT, N_ISTRANSFEREDIN, N_ISTRANSFEREDOUT, N_ISDEATH, N_ISCRITICAL, N_ISSURGERY, N_ISLEVEL4SURGERY, N_ISBEDSIDE, N_ISSPECIAL, N_ISOTHER, N_ISGENERATED
, N_GENERATEDON, N_ILLNESSSTATEBRIEF, N_CREATEEMPLOYEEID, N_MODIFIEDEMPLOYEEID, N_ROWVERSION, N_ISDELETED, N_DIAGNOSIS, N_SURGERIES, N_NOTE, N_SICKBEDORDINAL
, USERNAME, IP)
VALUES(:old.DOCTORSHIFTRECORDDETAILID, :old.DOCTORSHIFTRECORDID, :old.ORDINAL, :old.ENCOUNTERID, :old.SICKBEDID, :old.SICKBEDNO, :old.DISPLAYNAME, :old.BIRTHTIME, :old.GENDERCODEID, :old.GENDERDESC, :old.SEQNOTEXT
, :old.ISNEWINPATIENT, :old.ISOUTPATIENT, :old.ISTRANSFEREDIN, :old.ISTRANSFEREDOUT, :old.ISDEATH, :old.ISCRITICAL, :old.ISSURGERY, :old.ISLEVEL4SURGERY, :old.ISBEDSIDE, :old.ISSPECIAL, :old.ISOTHER, :old.ISGENERATED
, :old.GENERATEDON, :old.ILLNESSSTATEBRIEF, :old.CREATEEMPLOYEEID, :old.MODIFIEDEMPLOYEEID, :old.ROWVERSION, :old.ISDELETED, :old.DIAGNOSIS, :old.SURGERIES, :old.NOTE, :old.SICKBEDORDINAL
, NULLIF(:new.DOCTORSHIFTRECORDID, :old.DOCTORSHIFTRECORDID), NULLIF(:new.ORDINAL, :old.ORDINAL), NULLIF(:new.ENCOUNTERID, :old.ENCOUNTERID), NULLIF(:new.SICKBEDID, :old.SICKBEDID), NULLIF(:new.SICKBEDNO, :old.SICKBEDNO)
, NULLIF(:new.DISPLAYNAME, :old.DISPLAYNAME), NULLIF(:new.BIRTHTIME, :old.BIRTHTIME), NULLIF(:new.GENDERCODEID, :old.GENDERCODEID), NULLIF(:new.GENDERDESC, :old.GENDERDESC), NULLIF(:new.SEQNOTEXT, :old.SEQNOTEXT)
, NULLIF(:new.ISNEWINPATIENT, :old.ISNEWINPATIENT), NULLIF(:new.ISOUTPATIENT, :old.ISOUTPATIENT), NULLIF(:new.ISTRANSFEREDIN, :old.ISTRANSFEREDIN), NULLIF(:new.ISTRANSFEREDOUT, :old.ISTRANSFEREDOUT), NULLIF(:new.ISDEATH, :old.ISDEATH)
, NULLIF(:new.ISCRITICAL, :old.ISCRITICAL), NULLIF(:new.ISSURGERY, :old.ISSURGERY), NULLIF(:new.ISLEVEL4SURGERY, :old.ISLEVEL4SURGERY), NULLIF(:new.ISBEDSIDE, :old.ISBEDSIDE), NULLIF(:new.ISSPECIAL, :old.ISSPECIAL)
, NULLIF(:new.ISOTHER, :old.ISOTHER), NULLIF(:new.ISGENERATED, :old.ISGENERATED), NULLIF(:new.GENERATEDON, :old.GENERATEDON), NULLIF(:new.ILLNESSSTATEBRIEF, :old.ILLNESSSTATEBRIEF), NULLIF(:new.CREATEEMPLOYEEID, :old.CREATEEMPLOYEEID)
, NULLIF(:new.MODIFIEDEMPLOYEEID, :old.MODIFIEDEMPLOYEEID), NULLIF(:new.ROWVERSION, :old.ROWVERSION), NULLIF(:new.ISDELETED, :old.ISDELETED), NULLIF(:new.DIAGNOSIS, :old.DIAGNOSIS), NULLIF(:new.SURGERIES, :old.SURGERIES), NULLIF(:new.NOTE, :old.NOTE), NULLIF(:new.SICKBEDORDINAL, :old.SICKBEDORDINAL)
, v_user, v_ip);
END if;
END trg_updDOCTORSHIFTRECORDDETAIL;