如何在Oracle 11g中使用游标插入行

时间:2011-11-05 08:20:40

标签: oracle database-cursor

我正在尝试在oracle中编写一个函数,它将使用游标在其中一个表中插入数据。我们需要选择查询来选择需要插入的数据。这是我写的第一个光标,它变得过于复杂。

我的光标如下:

/* Formatted on 11/5/2011 11:26:57 AM (QP5 v5.149.1003.31008) */
DECLARE
    CURSOR csgetpgmecultstrecrefrs (
        update_date       DATE,
        sequence_type     VARCHAR2,
        pip_number        VARCHAR2,
        startfrom         INT,
        endon             INT)
    IS
        SELECT                                   /*+first_rows(25)  parallel (PE,20) */
                pecu.component,
                 pecu.component_serial_no,
                 TO_DATE ('11/03/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
                     date_received,
                 TO_DATE ('11/03/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
                     date_programmed,
                 pecu.date_requested,
                 pecu.component_model_no,
                 pecu.product_type,
                 pecu.product_model_no,
                 pecu.product_serial_no,
                 pecu.factory_source,
                 pecu.programming_organization,
                 pecu.programming_site,
                 pecu.program_version,
                 pecu.ecu_serial_no,
                 pecu.ecu_part_no,
                 pecu.ecu_level,
                 pecu.software_assembly_id,
                 jdcp_pip_swa.pip_version software_assembly_id_upgrade,
                 TO_DATE ('11/03/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
                     software_upgrade_date,
                 jdcp_pip_details.released_by software_upgraded_by,
                 pecu.power_bump,
                 pecu.fuel_system_part_no,
                 pecu.fuel_pump_serial_no,
                 pecu.rack_slope,
                 pecu.off_set,
                 pecu.ecu_hours,
                 pecu.cal_file,
                 pecu.ecu_boot_block_part_no,
                 jdcp_pip_details.released_by user_id,
                 pecu.performance_option_part_no,
                 pecu.vehicle_option_part_no,
                 'PIP' sequence_type,
                 pecu.flex_power_part_no,
                 pecu.performance_option_cd,
                 pecu.vehicle_option_cd,
                 pecu.lineage,
                 pecu.replaced_by_esn,
                 pecu.replaces_esn,
                 pecu.esn_copied_from,
                 pecu.payload_seq_no,
                 pecu.vehicle_system_id,
                 pecu.user_account,
                 pecu.ecu_opcode_part_no,
                 pecu.opcode_compat_code,
                 pecu.marked_qa
          FROM   (   (  jdcp_pip_details
                            INNER JOIN
                                jdcp_pip_pin_assc
                            ON jdcp_pip_details.pip_number =
                                    jdcp_pip_pin_assc.pip_number)
                      INNER JOIN
                          jdcp_pip_swa
                      ON jdcp_pip_details.pip_swa_id = jdcp_pip_swa.pip_swa_id)
                 INNER JOIN
                     (   (SELECT pe.component,
                                     pe.component_serial_no,
                                     pe.display_serial_no_13,
                                     pe.display_serial_no_17,
                                     pe.date_requested,
                                     pe.component_model_no,
                                     pe.product_type,
                                     pe.product_model_no,
                                     pe.product_serial_no,
                                     pe.factory_source,
                                     pe.programming_organization,
                                     pe.programming_site,
                                     pe.program_version,
                                     pe.ecu_serial_no,
                                     pe.ecu_part_no,
                                     pe.ecu_level,
                                     pe.software_assembly_id,
                                     pe.power_bump,
                                     pe.fuel_system_part_no,
                                     pe.fuel_pump_serial_no,
                                     pe.rack_slope,
                                     pe.off_set,
                                     pe.ecu_hours,
                                     pe.cal_file,
                                     pe.ecu_boot_block_part_no,
                                     pe.performance_option_part_no,
                                     pe.vehicle_option_part_no,
                                     pe.flex_power_part_no,
                                     pe.performance_option_cd,
                                     pe.vehicle_option_cd,
                                     pe.lineage,
                                     pe.replaced_by_esn,
                                     pe.replaces_esn,
                                     pe.esn_copied_from,
                                     pe.payload_seq_no,
                                     pe.vehicle_system_id,
                                     pe.user_account,
                                     pe.ecu_opcode_part_no,
                                     pe.opcode_compat_code,
                                     pe.marked_qa
                              FROM programmed_ecu_13_17_map_view pe
                             WHERE pe.date_received =
                                         (SELECT /*+ INDEX_DESC(PEDR PROGRAMMED_ECU_INDEX8) */
                                                  pedr.date_received AS date_received
                                             FROM programmed_ecu pedr
                                            WHERE pedr.component_serial_no =
                                                        pe.component_serial_no
                                                    AND pedr.component = pe.component
                                                    AND (pedr.date_programmed) =
                                                             (SELECT /*+ INDEX_DESC(PEDP PROGRAMMED_ECU_INDEX8) */
                                                                      pedp.date_programmed
                                                                 FROM programmed_ecu pedp
                                                                WHERE pedp.
                                                                         component_serial_no =
                                                                            pedr.
                                                                             component_serial_no
                                                                        AND pedp.component =
                                                                                 pedr.component
                                                                        AND ROWNUM = 1)
                                                    AND ROWNUM = 1)) pecu
                      INNER JOIN
                          software_assembly_id
                      ON pecu.vehicle_system_id =
                              software_assembly_id.vehicle_system_id)
                 ON (pecu.component = jdcp_pip_pin_assc.controller_short_name)
                     AND (jdcp_pip_swa.pip_version =
                                software_assembly_id.software_assembly_id)
                     AND (pecu.component =
                                software_assembly_id.controller_short_name)
         WHERE (pecu.display_serial_no_13 = jdcp_pip_pin_assc.pin_number
                  OR pecu.display_serial_no_17 = jdcp_pip_pin_assc.pin_number)
                 AND pecu.software_assembly_id <> jdcp_pip_swa.pip_version
                 AND jdcp_pip_details.pip_number = 'TEST_FWD_ASSC'
                 AND jdcp_pip_pin_assc.status_cd NOT IN
                          ('UC', 'SU', 'FA', 'RP', 'EC')
                 AND jdcp_pip_pin_assc.forward_associated = 'N'
                 AND ROWNUM BETWEEN 1 AND 25;

    rc   csgetpgmecultstrecrefrs%ROWTYPE;
BEGIN
    OPEN csgetpgmecultstrecrefrs (update_date date,
                                            sequence_type varchar2,
                                            pip_number varchar2,
                                            startfrom int,
                                            endon int);
LOOP
    FETCH csgetpgmecultstrecrefrs BULK COLLECT INTO rc;

    EXIT WHEN csgetpgmecultstrecrefrs%NOTFOUND;

    INSERT
      INTO programmed_ecu (component,
                                  component_serial_no,
                                  date_received,
                                  date_programmed,
                                  date_requested,
                                  component_model_no,
                                  product_type,
                                  product_model_no,
                                  product_serial_no,
                                  factory_source,
                                  programming_organization,
                                  programming_site,
                                  program_version,
                                  ecu_serial_no,
                                  ecu_part_no,
                                  ecu_level,
                                  software_assembly_id,
                                  software_assembly_id_upgrade,
                                  software_upgrade_date,
                                  software_upgraded_by,
                                  power_bump,
                                  fuel_system_part_no,
                                  fuel_pump_serial_no,
                                  rack_slope,
                                  off_set,
                                  ecu_hours,
                                  cal_file,
                                  ecu_boot_block_part_no,
                                  user_id,
                                  performance_option_part_no,
                                  vehicle_option_part_no,
                                  sequence_type,
                                  flex_power_part_no,
                                  performance_option_cd,
                                  vehicle_option_cd,
                                  lineage,
                                  replaced_by_esn,
                                  replaces_esn,
                                  esn_copied_from,
                                  payload_seq_no,
                                  vehicle_system_id,
                                  user_account,
                                  ecu_opcode_part_no,
                                  opcode_compat_code,
                                  marked_qa
                                 )
    VALUES (
                 rc.component,
                 rc.component_serial_no,
                 rc.date_received,
                 rc.date_programmed,
                 rc.date_requested,
                 rc.component_model_no,
                 rc.product_type,
                 rc.product_model_no,
                 rc.product_serial_no,
                 rc.factory_source,
                 rc.programming_organization,
                 rc.programming_site,
                 rc.program_version,
                 rc.ecu_serial_no,
                 rc.ecu_part_no,
                 rc.ecu_level,
                 rc.software_assembly_id,
                 rc.software_assembly_id_upgrade,
                 rc.software_upgrade_date,
                 rc.software_upgraded_by,
                 rc.power_bump,
                 rc.fuel_system_part_no,
                 rc.fuel_pump_serial_no,
                 rc.rack_slope,
                 rc.off_set,
                 rc.ecu_hours,
                 rc.cal_file,
                 rc.ecu_boot_block_part_no,
                 rc.user_id,
                 rc.performance_option_part_no,
                 rc.vehicle_option_part_no,
                 rc.sequence_type,
                 rc.flex_power_part_no,
                 rc.performance_option_cd,
                 rc.vehicle_option_cd,
                 rc.lineage,
                 rc.replaced_by_esn,
                 rc.replaces_esn,
                 rc.esn_copied_from,
                 rc.payload_seq_no,
                 rc.vehicle_system_id,
                 rc.user_account,
                 rc.ecu_opcode_part_no,
                 rc.opcode_compat_code,
                 rc.marked_qa);
END LOOP;

COMMIT;
END;

感谢任何帮助。提前致谢 !!! 我得到的错误信息是:

  

第2行出错   ORA-06550:第147行,第44栏:   PLS-00103:遇到以下其中一项时遇到符号“DATE”:   。 (),* @%&amp; | = - +&lt; /&gt; at是mod的余数不是   范围rem =&gt; ..&lt;&gt;或!=或〜=&gt; =&lt; =&lt;&gt;   和/或喜欢使用||之间的LIKE2_ LIKE4_ LIKEC_   multiset成员SUBMULTISET_   ORA-06550:第151行,第21栏:   PLS-00103:遇到以下其中一项时遇到符号“)”:   ,从批量   ORA-06550:第254行,第4栏:   PLS-00103:遇到以下任何一种情况时遇到符号“文件结束”:

     

结束不是pragma最终可实例化的命令覆盖静态   成员构造函数映射   脚本终止于第2行。“

1 个答案:

答案 0 :(得分:1)

光标的声明不正确。未使用数据类型声明游标,您需要删除括号之间的整个部分。每列的数据类型由SELECT语句返回的列确定:

CURSOR csgetpgmecultstrecrefrs 
IS
SELECT .....

有关详细信息,请参阅手册中的示例:

http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/sqloperations.htm#sthref1296