有张病人信息表,经常会无故丢失字段内容或者字段内容改变,没找到原因。
考虑增加触发器跟踪,出现问题的电脑ip,操作员和程序/界面。
通过SYS_CONTEXT函数可以调用到所需信息,并通过insert/update触发器记录到其他表。
例如update触发器:
Create Or Replace Trigger MEDREC.update_ls
Before UPDATE
ON MEDREC.pat_visit
FOR EACH ROW
BEGIN
INSERT INTO pat_visit_t1
(ADMISSION_CAUSE,ADMISSION_DATE_TIME,ADMITTED_BY,ADVANCED_STUDIES_DOCTOR,ADVERSE_REACTION_DRUGS,ALERGY_DRUGS,ARMED_SERVICES,ATTENDING_DOCTOR,AUTOPSY_INDICATOR,BLOOD_TRAN_REACT_TIMES,BLOOD_TRAN_TIMES,BLOOD_TRAN_VOL,BLOOD_TYPE,BLOOD_TYPE_RH,CATALOGER,CATALOG_DATE,CCU_DAYS,CHARGE_TYPE,CHIEF_DOCTOR,CONSULTING_DATE,CONSULTING_DOCTOR,CRITICAL_COND_DAYS,DATE_OF_CONTROL_QUALITY,DECUBITAL_ULCER_TIMES,DEPT_ADMISSION_TO,DEPT_DISCHARGE_FROM,DIRECTOR,DISCHARGE_DATE_TIME,DISCHARGE_DISPOSITION,DOCTOR_IN_CHARGE,DOCTOR_OF_CONTROL_QUALITY,DUTY,EMER_TREAT_TIMES,ESC_EMER_TIMES,FIRST_CASE_INDICATOR,FIRST_LEVEL_NURS_DAYS,FOLLOW_INDICATOR,FOLLOW_INTERVAL,FOLLOW_INTERVAL_UNITS,HBSAG_INDICATOR,HCV_AB_INDICATOR,HIV_AB_INDICATOR,ICU_DAYS,IDENTITY,IDENTITY_CLASS,INFUSION_REACT_TIMES,INSURANCE_NO,INSURANCE_TYPE,INTERNAL_NO,MAILING_ADDRESS,MARITAL_STATUS,MEDICAL_PAY_WAY,MR_QUALITY,MR_VALUE,NEXT_OF_KIN,NEXT_OF_KIN_ADDR,NEXT_OF_KIN_PHONE,NEXT_OF_KIN_ZIPCODE,NURSE_OF_CONTROL_QUALITY,OCCUPATION,PATIENT_CLASS,PATIENT_ID,PAT_ADM_CONDITION,PRACTICE_DOCTOR,PRACTICE_DOCTOR_OF_GRADUATE,RELATIONSHIP,SECOND_LEVEL_NURS_DAYS,SERIOUS_COND_DAYS,SERVICE_AGENCY,SERVICE_SYSTEM_INDICATOR,SPEC_LEVEL_NURS_DAYS,THIRD_LEVEL_NURS_DAYS,TOP_UNIT,TOTAL_COSTS,TOTAL_PAYMENTS,TRAINING_INJURY_INDICATOR,UNIT_IN_CONTRACT,VISIT_ID,WORKING_STATUS,X_EXAM_NO,ZIP_CODE,CHANGE_DATE,CHANGE_USER,CHANGE_ACTION,CHANGE_MODULE,CHANGE_IP)
VALUES
(:new.ADMISSION_CAUSE,:new.ADMISSION_DATE_TIME,:new.ADMITTED_BY,:new.ADVANCED_STUDIES_DOCTOR,:new.ADVERSE_REACTION_DRUGS,:new.ALERGY_DRUGS,:new.ARMED_SERVICES,:new.ATTENDING_DOCTOR,:new.AUTOPSY_INDICATOR,:new.BLOOD_TRAN_REACT_TIMES,:new.BLOOD_TRAN_TIMES,:new.BLOOD_TRAN_VOL,:new.BLOOD_TYPE,:new.BLOOD_TYPE_RH,:new.CATALOGER,:new.CATALOG_DATE,:new.CCU_DAYS,:new.CHARGE_TYPE,:new.CHIEF_DOCTOR,:new.CONSULTING_DATE,:new.CONSULTING_DOCTOR,:new.CRITICAL_COND_DAYS,:new.DATE_OF_CONTROL_QUALITY,:new.DECUBITAL_ULCER_TIMES,:new.DEPT_ADMISSION_TO,:new.DEPT_DISCHARGE_FROM,:new.DIRECTOR,:new.DISCHARGE_DATE_TIME,:new.DISCHARGE_DISPOSITION,:new.DOCTOR_IN_CHARGE,:new.DOCTOR_OF_CONTROL_QUALITY,:new.DUTY,:new.EMER_TREAT_TIMES,:new.ESC_EMER_TIMES,:new.FIRST_CASE_INDICATOR,:new.FIRST_LEVEL_NURS_DAYS,:new.FOLLOW_INDICATOR,:new.FOLLOW_INTERVAL,:new.FOLLOW_INTERVAL_UNITS,:new.HBSAG_INDICATOR,:new.HCV_AB_INDICATOR,:new.HIV_AB_INDICATOR,:new.ICU_DAYS,:new.IDENTITY,:new.IDENTITY_CLASS,:new.INFUSION_REACT_TIMES,:new.INSURANCE_NO,:new.INSURANCE_TYPE,:new.INTERNAL_NO,:new.MAILING_ADDRESS,:new.MARITAL_STATUS,:new.MEDICAL_PAY_WAY,:new.MR_QUALITY,:new.MR_VALUE,:new.NEXT_OF_KIN,:new.NEXT_OF_KIN_ADDR,:new.NEXT_OF_KIN_PHONE,:new.NEXT_OF_KIN_ZIPCODE,:new.NURSE_OF_CONTROL_QUALITY,:new.OCCUPATION,:new.PATIENT_CLASS,:new.PATIENT_ID,:new.PAT_ADM_CONDITION,:new.PRACTICE_DOCTOR,:new.PRACTICE_DOCTOR_OF_GRADUATE,:new.RELATIONSHIP,:new.SECOND_LEVEL_NURS_DAYS,:new.SERIOUS_COND_DAYS,:new.SERVICE_AGENCY,:new.SERVICE_SYSTEM_INDICATOR,:new.SPEC_LEVEL_NURS_DAYS,:new.THIRD_LEVEL_NURS_DAYS,:new.TOP_UNIT,:new.TOTAL_COSTS,:new.TOTAL_PAYMENTS,:new.TRAINING_INJURY_INDICATOR,:new.UNIT_IN_CONTRACT,:new.VISIT_ID,:new.WORKING_STATUS,:new.X_EXAM_NO,:new.ZIP_CODE,to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss'),sys_context('userenv','session_user'),SYS_CONTEXT('USERENV','ACTION'),SYS_CONTEXT('USERENV','MODULE'),SYS_CONTEXT('USERENV','IP_ADDRESS') );
END;
下面是SYS_CONTEXT函数的用法(以下为转载,方便查找):
大家运行一下就能大概知道每个参数的意思了。其中有些参数在其他视图中也可以查询出来的,如:nls_session_parameters、v$serssion、v$parameter、v$instance(上面绿色的部分),但大部分参数只能通过这种方式查询。