如何使用多个游标从oracle过程返回所有行

时间:2011-12-09 14:00:45

标签: oracle stored-procedures plsql oracle10g

我有5个表,使用oracle过程如何从这五个表中获取所选列,并将所有行作为带有连接的简单选择查询返回。我写过这个程序,但我无法得到理想的结果。

    create or replace
    PROCEDURE NOTIFICATIN_REPORT_TEST(
        para_claim_no IN VARCHAR2,
        O_CRM_ACC_CAL_GARAGE out CRM_CLAIM_INT_DETAILS_VIEW.CRM_ACC_CAL_GARAGE%type,
    O_CRM_ACC_DAM_COMP out CRM_CLAIM_INT_DETAILS_VIEW.CRM_ACC_DAM_COMP%type,
    O_CRM_ACC_DATE_TIME out CRM_CLAIM_INT_DETAILS_VIEW.CRM_ACC_DATE_TIME%type,
    O_CRM_ACC_DESC out CRM_CLAIM_INT_DETAILS_VIEW.CRM_ACC_DESC%type,
    O_CRM_ACC_FIR out CRM_CLAIM_INT_DETAILS_VIEW.CRM_ACC_FIR%type,
    O_CRM_ACC_INJ_DET_TPPD out CRM_CLAIM_INT_DETAILS_VIEW.CRM_ACC_INJ_DET_TPPD%type,
    O_CRM_DRI_LICENSE_NO out CRM_CLAIM_INT_DETAILS_VIEW.CRM_DRI_LICENSE_NO%type,
    O_CRM_DRI_NAME out CRM_CLAIM_INT_DETAILS_VIEW.CRM_DRI_NAME%type,
    O_CRM_DRI_RELATION out CRM_CLAIM_INT_DETAILS_VIEW.CRM_DRI_RELATION%type,
    O_CRM_DRI_VALID_UPTO out CRM_CLAIM_INT_DETAILS_VIEW.CRM_DRI_VALID_UPTO%type,
    O_CRM_INS_ADDRESS out CRM_CLAIM_INT_DETAILS_VIEW.CRM_INS_ADDRESS%type,
    O_CRM_INS_CLIENT_ID out CRM_CLAIM_INT_DETAILS_VIEW.CRM_INS_CLIENT_ID%type,
    O_CRM_INS_CONTACT_NO out CRM_CLAIM_INT_DETAILS_VIEW.CRM_INS_CONTACT_NO%type,
    O_CRM_INS_MOBILE_NO out CRM_CLAIM_INT_DETAILS_VIEW.CRM_INS_MOBILE_NO%type,
    O_CRM_INS_NAME out CRM_CLAIM_INT_DETAILS_VIEW.CRM_INS_NAME%type,
    O_CRM_INS_OFFICE out CRM_CLAIM_INT_DETAILS_VIEW.CRM_INS_OFFICE%type,
    O_CRM_INS_VEHICLE_NO out CRM_CLAIM_INT_DETAILS_VIEW.CRM_INS_VEHICLE_NO%type,
    O_CRM_POLICE_STN_NAME out CRM_CLAIM_INT_DETAILS_VIEW.CRM_POLICE_STN_NAME%type,
    O_CRM_REMARKS out CRM_CLAIM_INT_DETAILS_VIEW.CRM_REMARKS%type,
    O_CRM_VEH_USED_FOR out CRM_CLAIM_INT_DETAILS_VIEW.CRM_VEH_USED_FOR%type,
    O_ADDRESSLINE1 out INTRFC_MOTOR_NOTIFICATION_VIEW.ADDRESSLINE1%type,
    O_ADDRESSLINE2 out INTRFC_MOTOR_NOTIFICATION_VIEW.ADDRESSLINE2%type,
    O_CITYDISTRICT_NAME out INTRFC_MOTOR_NOTIFICATION_VIEW.CITYDISTRICT_NAME%type,
    O_CONTACTPERSON_NAME out INTRFC_MOTOR_NOTIFICATION_VIEW.CONTACTPERSON_NAME%type,
    O_COUNTRY_NAME out INTRFC_MOTOR_NOTIFICATION_VIEW.COUNTRY_NAME%type,
    O_EMAIL_ID out INTRFC_MOTOR_NOTIFICATION_VIEW.EMAIL_ID%type,
    O_LANDMARK out INTRFC_MOTOR_NOTIFICATION_VIEW.LANDMARK%type,
    O_MOBILE_NUMBER out INTRFC_MOTOR_NOTIFICATION_VIEW.MOBILE_NUMBER%type,
    O_NATUREOFLOSS_DESCRIPTION out INTRFC_MOTOR_NOTIFICATION_VIEW.NATUREOFLOSS_DESCRIPTION%type,
    O_PINCODE out INTRFC_MOTOR_NOTIFICATION_VIEW.PINCODE%type,
    O_POLICY_NUM out INTRFC_MOTOR_NOTIFICATION_VIEW.POLICY_NUM%type,
    O_RELATION_WITH_CUSTOMER out INTRFC_MOTOR_NOTIFICATION_VIEW.RELATION_WITH_CUSTOMER%type,
    O_REPAIRER_NAME out INTRFC_MOTOR_NOTIFICATION_VIEW.REPAIRER_NAME%type,
    O_STATE_NAME out INTRFC_MOTOR_NOTIFICATION_VIEW.STATE_NAME%type,
    O_TEL_NUMBER out INTRFC_MOTOR_NOTIFICATION_VIEW.TEL_NUMBER%type,
    O_VILLAGE_NAME out INTRFC_MOTOR_NOTIFICATION_VIEW.VILLAGE_NAME%type,
    O_MAKE out INTRFC_MOTOR_RISK_VIEW.MAKE%type,
    O_MODEL out INTRFC_MOTOR_RISK_VIEW.MODEL%type,
    O_AGENT_NAME out INTRFC_POLICY_VIEW.AGENT_NAME%type,
    O_BRANCH_OFFICE_DESC out INTRFC_POLICY_VIEW.BRANCH_OFFICE_DESC%type,
    O_MANUAL_COVERNOTE_NO out INTRFC_POLICY_VIEW.MANUAL_COVERNOTE_NO%type,
    O_RISK_END_DATE out INTRFC_POLICY_VIEW.RISK_END_DATE%type,
    O_RISK_START_DATE out INTRFC_POLICY_VIEW.RISK_START_DATE%type,
    O_CHASIS_NO out MO_CLAIM_MASTER.CHASIS_NO%type,
    O_DATE_OF_NOTIFICATION out MO_CLAIM_MASTER.DATE_OF_NOTIFICATION%type,
    O_ENGINE_NO out MO_CLAIM_MASTER.ENGINE_NO%type,
    O_NOTF_FIR_DATE out MO_CLAIM_MASTER.NOTF_FIR_DATE%type,
    O_NOTIF_ADDRES_POLICE_STATN out MO_CLAIM_MASTER.NOTIF_ADDRES_POLICE_STATN%type,
    O_NOTIF_CP_EMAIL out MO_CLAIM_MASTER.NOTIF_CP_EMAIL%type,
    O_NOTIF_DRIVER_OTHER_INFO out MO_CLAIM_MASTER.NOTIF_DRIVER_OTHER_INFO%type,
    O_NOTIF_LOSS_LOC_TYPE out MO_CLAIM_MASTER.NOTIF_LOSS_LOC_TYPE%type,
    O_NOTIF_OTHER_REMARKS out MO_CLAIM_MASTER.NOTIF_OTHER_REMARKS%type,
    O_NOTIF_PLACE_PARKING out MO_CLAIM_MASTER.NOTIF_PLACE_PARKING%type,
    O_NOTIF_SURVEY_TO_DONE out MO_CLAIM_MASTER.NOTIF_SURVEY_TO_DONE%type,
    O_NOTIF_SVC_PROV_CONTACT_NUM out MO_CLAIM_MASTER.NOTIF_SVC_PROV_CONTACT_NUM%type,
    O_NOTIF_SVC_PROV_INFORMED out MO_CLAIM_MASTER.NOTIF_SVC_PROV_INFORMED%type,
    O_NOTIF_SVC_PROV_NAME out MO_CLAIM_MASTER.NOTIF_SVC_PROV_NAME%type,
    O_NOTIF_TOYOTA_POLICY_NO out MO_CLAIM_MASTER.NOTIF_TOYOTA_POLICY_NO%type,
    O_PRODUCT_CODE out MO_CLAIM_MASTER.PRODUCT_CODE%type,
    O_STATUS out MO_CLAIM_MASTER.STATUS%type


         )
    AS
      reference_num_var VARCHAR2(50);
      claim_no_var      NUMBER;
      crm_sl_no_var     NUMBER;
      CURSOR c1
      IS
        SELECT NV.claim_no,
          NV.reference_num,
          NV.ADDRESSLINE1,
          NV.ADDRESSLINE2,
          NV.CITYDISTRICT_NAME,
          NV.CONTACTPERSON_NAME,
          NV.COUNTRY_NAME,
          NV.EMAIL_ID,
          NV.LANDMARK,
          NV.MOBILE_NUMBER,
          NV.NATUREOFLOSS_DESCRIPTION,
          NV.PINCODE,
          NV.POLICY_NUM,
          NV.RELATION_WITH_CUSTOMER,
          NV.REPAIRER_NAME,
          NV.STATE_NAME,
          NV.TEL_NUMBER,
          NV.VILLAGE_NAME
        INTO claim_no_var,
          reference_num_var,
          O_ADDRESSLINE1,
          O_ADDRESSLINE2,
          O_CITYDISTRICT_NAME,
          O_CONTACTPERSON_NAME,
          O_COUNTRY_NAME,
          O_EMAIL_ID,
          O_LANDMARK,
          O_MOBILE_NUMBER,
          O_NATUREOFLOSS_DESCRIPTION,
          O_PINCODE,
          O_POLICY_NUM,
          O_RELATION_WITH_CUSTOMER,
          O_REPAIRER_NAME,
          O_STATE_NAME,
          O_TEL_NUMBER,
          O_VILLAGE_NAME
        FROM INTRFC_MOTOR_NOTIFICATION_VIEW NV
          --WHERE NV.claim_no=para_claim_no;
        WHERE NV.POLICY_NUM=para_claim_no ;
      t1 c1%rowtype;
      CURSOR c2
      IS
        SELECT PV.AGENT_NAME,
          PV.BRANCH_OFFICE_DESC,
          PV.MANUAL_COVERNOTE_NO,
          PV.RISK_END_DATE,
          PV.RISK_START_DATE
          INTO
          O_AGENT_NAME,
    O_BRANCH_OFFICE_DESC,
    O_MANUAL_COVERNOTE_NO,
    O_RISK_END_DATE,
    O_RISK_START_DATE

        FROM INTRFC_POLICY_VIEW PV
        WHERE PV.NUM_REFERENCE_NUMBER=reference_num_var;
      t2 c2%rowtype;
      CURSOR c3
      IS
        SELECT RV.MAKE,
          RV.MODEL
          INTO
          O_MAKE,
    O_MODEL
        FROM INTRFC_MOTOR_RISK_VIEW RV
        WHERE RV.reference_num=reference_num_var;
      t3 c3%rowtype;
      CURSOR c4
      IS
        SELECT CM.notif_crm_sl_no,
          CM.CHASIS_NO,
          CM.DATE_OF_NOTIFICATION,
          CM.ENGINE_NO,
          CM.NOTF_FIR_DATE,
          CM.NOTIF_ADDRES_POLICE_STATN,
          CM.NOTIF_CP_EMAIL,
          CM.NOTIF_DRIVER_OTHER_INFO,
          CM.NOTIF_LOSS_LOC_TYPE,
          CM.NOTIF_OTHER_REMARKS,
          CM.NOTIF_PLACE_PARKING,
          CM.NOTIF_SURVEY_TO_DONE,
          CM.NOTIF_SVC_PROV_CONTACT_NUM,
          CM.NOTIF_SVC_PROV_INFORMED,
          CM.NOTIF_SVC_PROV_NAME,
          CM.NOTIF_TOYOTA_POLICY_NO,
          CM.PRODUCT_CODE,
          CM.STATUS
          INTO
          crm_sl_no_var,
          O_CHASIS_NO,
    O_DATE_OF_NOTIFICATION,
    O_ENGINE_NO,
    O_NOTF_FIR_DATE,
    O_NOTIF_ADDRES_POLICE_STATN,
    O_NOTIF_CP_EMAIL,
    O_NOTIF_DRIVER_OTHER_INFO,
    O_NOTIF_LOSS_LOC_TYPE,
    O_NOTIF_OTHER_REMARKS,
    O_NOTIF_PLACE_PARKING,
    O_NOTIF_SURVEY_TO_DONE,
    O_NOTIF_SVC_PROV_CONTACT_NUM,
    O_NOTIF_SVC_PROV_INFORMED,
    O_NOTIF_SVC_PROV_NAME,
    O_NOTIF_TOYOTA_POLICY_NO,
    O_PRODUCT_CODE,
    O_STATUS


        FROM MO_CLAIM_MASTER CM
        WHERE CM.claim_no = claim_no_var;
      t4 c4%rowtype;
      CURSOR c5
      IS
        SELECT CRM.CRM_ACC_CAL_GARAGE,
          CRM.CRM_ACC_DAM_COMP,
          CRM.CRM_ACC_DATE_TIME,
          CRM.CRM_ACC_DESC,
          CRM.CRM_ACC_FIR,
          CRM.CRM_ACC_INJ_DET_TPPD,
          CRM.CRM_DRI_LICENSE_NO,
          CRM.CRM_DRI_NAME,
          CRM.CRM_DRI_RELATION,
          CRM.CRM_DRI_VALID_UPTO,
          CRM.CRM_INS_ADDRESS,
          CRM.CRM_INS_CLIENT_ID,
          CRM.CRM_INS_CONTACT_NO,
          CRM.CRM_INS_MOBILE_NO,
          CRM.CRM_INS_NAME,
          CRM.CRM_INS_OFFICE,
          CRM.CRM_INS_VEHICLE_NO,
          CRM.CRM_POLICE_STN_NAME,
          CRM.CRM_REMARKS,
          CRM.CRM_VEH_USED_FOR
          INTO
          O_CRM_ACC_CAL_GARAGE,
    O_CRM_ACC_DAM_COMP,
    O_CRM_ACC_DATE_TIME,
    O_CRM_ACC_DESC,
    O_CRM_ACC_FIR,
    O_CRM_ACC_INJ_DET_TPPD,
    O_CRM_DRI_LICENSE_NO,
    O_CRM_DRI_NAME,
    O_CRM_DRI_RELATION,
    O_CRM_DRI_VALID_UPTO,
    O_CRM_INS_ADDRESS,
    O_CRM_INS_CLIENT_ID,
    O_CRM_INS_CONTACT_NO,
    O_CRM_INS_MOBILE_NO,
    O_CRM_INS_NAME,
    O_CRM_INS_OFFICE,
    O_CRM_INS_VEHICLE_NO,
    O_CRM_POLICE_STN_NAME,
    O_CRM_REMARKS,
    O_CRM_VEH_USED_FOR
        FROM CRM_CLAIM_INT_DETAILS_VIEW CRM
        WHERE crm.crm_slno=crm_sl_no_var;
      t5 c5%rowtype;
    BEGIN
     OPEN c1;
        FETCH c1 INTO t1;
        CLOSE c1;
        reference_num_var := t1.reference_num;
        claim_no_var      := t1.claim_no;
        OPEN c2;
        FETCH c2 INTO t2;
        CLOSE c2;
        OPEN c3;
        FETCH c3 INTO t3;
        CLOSE c3;
        OPEN c4;
        FETCH c4 INTO t4;
        CLOSE c4;
        crm_sl_no_var :=T4.notif_crm_sl_no;
        OPEN c5;
        FETCH c5 INTO t5;
        CLOSE c5;
    END NOTIFICATIN_REPORT_TEST;

1 个答案:

答案 0 :(得分:3)

您可以使用自定义数据类型和流水线函数。 您为“记录”模板创建了一个类型:

CREATE OR REPLACE
TYPE O_MY_OBJECT AS OBJECT (
  MY_DATE                       DATE
, MY_NUMBER                     NUMBER
, MY_VARCHAR                    VARCHAR2(20)
);

然后为记录模板的表创建一个类型:

CREATE OR REPLACE
TYPE T_MY_OBJECT AS TABLE OF O_MY_OBJECT;

然后创建一个返回表对象的流水线函数:

FUNCTION MY_FUNCTION(V_MY_PARAM IN VARCHAR2)
  RETURN  T_MY_OBJECT
  PIPELINED
AS
V_MY_OBJ_VAR O_MY_OBJECT := O_MY_OBJECT(null, null, null);
BEGIN
      SELECT
            SYSDATE
          , 10
          , 'HELLO WORLD! -> ' || V_MY_PARAM
      INTO
          V_MY_OBJ_VAR.MY_DATE
        , V_MY_OBJ_VAR.MY_NUMBER
        , V_MY_OBJ_VAR.MY_VARCHAR
      FROM
          DUAL;

      PIPE ROW(V_MY_OBJ_VAR);

  RETURN;
END MY_FUNCTION;

在您的情况下,每次需要时,您将从五个游标的循环中的每一个循环中输出一行。 创建此类函数后,您可以使用表查询从中获取记录:

SELECT * FROM TABLE(MY_FUNCTION('SOME ARGUMENT VALUE, IF NEEDED'));