用户EXCEPTION和GOTO时存储过程有错误

时间:2013-12-04 11:19:37

标签: oracle stored-procedures

我尝试用光标循环生成的行。并使用GOTO忽略某些行的进程。

CREATE OR REPLACE PROCEDURE Es_Release_Hold_Lot_Job_Data(p_group_id IN NUMBER, p_lot_numbers OUT VARCHAR2)
IS
    CURSOR tx_cur IS
        SELECT * FROM xx_vms.es_transaction_data WHERE group_id = p_group_id and status not in (2, 3) and transaction_type not in (10, 13, 14) FOR UPDATE;

v_mode_flag NUMBER;
v_alt_bom_designator VARCHAR2(30);
v_alt_routing_designator VARCHAR2(30);
v_user_id       NUMBER;
v_source_line_id NUMBER;
v_load_type NUMBER;
v_firm_planned_flag NUMBER;
v_start_quantity NUMBER;
v_tmp_count NUMBER:=0;
v_start_date DATE;
v_status_type NUMBER:=3;
BEGIN
    FOR TX IN tx_cur
        LOOP
            v_start_quantity := tx.quantity;
            v_start_date := tx.start_date;
            SELECT user_id INTO v_user_id
            FROM fnd_user
            WHERE user_name = TX.created_by_name;
            v_alt_bom_designator := null;
            v_alt_routing_designator := null;
            select count(1)
            into v_tmp_count
            from wip_discrete_jobs
            where primary_item_id = tx.inventory_item_id and lot_number = tx.lot_number and status_type = 6;
            IF(v_tmp_count != 0) THEN
                select start_quantity
                into v_start_quantity
                from wip_discrete_jobs
                where primary_item_id = tx.inventory_item_id and lot_number = tx.lot_number and status_type = 6;
            ELSE
                goto continue_label;
            END IF;
            v_load_type := 5;
            v_firm_planned_flag := 1;
            v_mode_flag := 1;
            v_source_line_id := TX.header_id;
            v_load_type := 6;
            v_firm_planned_flag := 2;
            select count(1)
            into v_tmp_count
            from wip_discrete_jobs
            where primary_item_id = tx.inventory_item_id and lot_number = tx.lot_number and status_type = 6;
            IF(v_tmp_count!=0) THEN
                select scheduled_start_date
                into v_start_date
                from wip_discrete_jobs
                where primary_item_id = tx.inventory_item_id and lot_number = tx.lot_number and status_type = 6;
            ELSE
                goto continue_label;
            END IF;
            --return list of lots are released hold automatically
            p_lot_numbers := p_lot_numbers || ',' || tx.lot_number;
            DELETE FROM wsm_lot_job_interface where header_id = tx.header_id;
            INSERT INTO WSM_LOT_JOB_INTERFACE
                (
                    ALLOW_EXPLOSION,
                    ALTERNATE_BOM_DESIGNATOR,
                    ALTERNATE_ROUTING_DESIGNATOR,
                    BOM_REFERENCE_ID,
                    COMPLETION_SUBINVENTORY,
                    CREATED_BY_NAME,
                    CREATION_DATE,
                    DESCRIPTION,
                    FIRM_PLANNED_FLAG,
                    FIRST_UNIT_START_DATE,
                    GROUP_ID,
                    HEADER_ID,
                    JOB_NAME,
                    LAST_UNIT_COMPLETION_DATE,
                    LAST_UPDATE_DATE,
                    LAST_UPDATED_BY_NAME,
                    LAST_UPDATED_BY,
                    LOAD_TYPE,
                    LOT_NUMBER,
                    MODE_FLAG,
                    NET_QUANTITY,
                    ORGANIZATION_ID,
                    PRIMARY_ITEM_ID,
                    PROCESS_PHASE,
                    PROCESS_STATUS,
                    ROUTING_REFERENCE_ID,
                    SOURCE_CODE,
                    SOURCE_LINE_ID,
                    START_QUANTITY,
                    STATUS_TYPE,
                    WIP_SUPPLY_TYPE,
                    ATTRIBUTE14,
                    ATTRIBUTE15
                    ,ATTRIBUTE1
                    ,ATTRIBUTE2
                    ,ATTRIBUTE3
                )
                VALUES
                (
                    'Y',                                -- ALLOW_EXPLOSION,
                    v_alt_bom_designator,                           -- ALTERNATE_BOM_DESIGNATOR
                    v_alt_routing_designator,                           -- ALTERNATE_ROUTING_DESIGNATOR
                    TX.bom_reference_id,                                    -- BOM_REFERENCE_ID
                    TX.SUBINVENTORY_CODE,                       -- COMPLETION_SUBINVENTORY,
                    TX.CREATED_BY_NAME,                         -- CREATED_BY_NAME,
                    SYSDATE,--CREATED_DATE,                         -- CREATION_DATE,
                    'VMS WIP Job',                          -- DESCRIPTION,
                    v_firm_planned_flag,     -- FIRM_PLANNED FLAG = 2 for update comp date                                  -- FIRM_PLANNED_FLAG,
                    v_start_date,--TX.start_date,                           -- FIRST_UNIT_START_DATE,
                    p_group_id,                                 -- GROUP_ID,
                    TX.header_id,                           -- HEADER_ID,
                    TX.lot_number,                          -- JOB_NAME,
                    TX.proj_out_date,                       -- LAST_UNIT_COMPLETION_DATE,
                    SYSDATE,--LAST_UPDATED_DATE,                            -- LAST_UPDATE_DATE,
                    TX.LAST_UPDATED_BY_NAME,                            -- LAST_UPDATED_BY_NAME,
                    v_user_id,                          -- LAST_UPDATED_BY_NAME,
                    v_load_type,    -- LOAD_TYPE 6 for update                               -- LOAD_TYPE,            5 = create a lot based job, 6 = Reschedule a lot based Job
                    TX.lot_number,                          -- LOT_NUMBER,
                    v_mode_flag,                                    -- MODE_FLAG,
                    TX.quantity,                            -- NET_QUANTITY,
                    42,                                 -- ORGANIZATION_ID,
                    TX.inventory_item_id,
                    2,                                  -- PROCESS_PHASE,  2=Validation
                    1,                                  -- PROCESS_STATUS, 1=Pending
                    TX.routing_reference_id,            -- ROUTING_REFERENCE_ID
                    TX.SOURCE_CODE,                             -- SOURCE_CODE,
                    v_source_line_id,                                   -- SOURCE_LINE_ID,
                    v_start_quantity,                           -- START_QUANTITY,
                    v_status_type,                                  -- STATUS_TYPE, 1= Unreleases, 3=Released, 6=Hold
                    7,                                  -- wip_supply_type = Based on Bill, WIP_SUPPLY_TYPE 1 = Push, 3 = Operation Pull
                    TO_CHAR(TX.group_id),
                    TO_CHAR(TX.header_id)
                    ,TX.po_number
                    ,TX.date_code
                    ,to_char(TX.initial_commit_date,'DD-MON-YY')
                );
            IF(v_mode_flag = 2) THEN
                DELETE FROM WSM_SPLIT_MERGE_TXN_INTERFACE
                WHERE
                HEADER_ID IN
                (
                    SELECT DISTINCT HEADER_ID
                    FROM WSM_STARTING_LOTS_INTERFACE
                    WHERE CONCAT(TO_CHAR(INVENTORY_ITEM_ID),LOT_NUMBER) IN
                           (
                                SELECT
                                        CONCAT(TO_CHAR(TO_NUMBER(C.INVENTORY_ITEM_ID)),A.LOT_NUMBER)
                                FROM
                                        XX_VMS.ES_TRANSACTION_CHILD_RECORDS A,
                                        XX_VMS.ES_TRANSACTION_DATA B,
                                        XX_VMS.ES_ITEM C
                                WHERE
                                        A.ES_TRANSACTION_ID = B.ID
                                AND     A.PART_NUMBER = C.PART_NUMBER
                                AND     B.GROUP_ID = p_group_id
                                AND     B.ID = TX.ID
                           )
                );

                DELETE FROM WSM_STARTING_LOTS_INTERFACE WHERE CONCAT(TO_CHAR(INVENTORY_ITEM_ID),LOT_NUMBER) IN
                       (
                            SELECT
                                    CONCAT(TO_CHAR(TO_NUMBER(C.INVENTORY_ITEM_ID)),A.LOT_NUMBER)
                            FROM
                                    XX_VMS.ES_TRANSACTION_CHILD_RECORDS A,
                                    XX_VMS.ES_TRANSACTION_DATA B,
                                    XX_VMS.ES_ITEM C
                            WHERE
                                    A.ES_TRANSACTION_ID = B.ID
                            AND     A.PART_NUMBER = C.PART_NUMBER
                            AND     B.GROUP_ID = p_group_id
                            AND     B.ID = TX.ID
                       );

                INSERT INTO WSM_STARTING_LOTS_INTERFACE
                (
                    CREATED_BY,
                    CREATION_DATE,
                    HEADER_ID,
                    INVENTORY_ITEM_ID,
                    LAST_UPDATE_DATE,
                    LAST_UPDATED_BY,
                    LOT_NUMBER,
                    ORGANIZATION_ID,
                    QUANTITY,
                    SUBINVENTORY_CODE
                )
                SELECT
                        F.USER_ID,
                        SYSDATE,--B.CREATED_DATE,
                        v_source_line_id,
                        C.INVENTORY_ITEM_ID,
                        B.LAST_UPDATED_DATE,
                        F.USER_ID,
                        A.LOT_NUMBER,
                        42,
                        A.QUANTITY,
                        A.SUBINVENTORY_CODE
                FROM
                        XX_VMS.ES_TRANSACTION_CHILD_RECORDS A,
                        XX_VMS.ES_TRANSACTION_DATA B,
                        XX_VMS.ES_ITEM C,
                        FND_USER F
                WHERE
                            A.ES_TRANSACTION_ID = B.ID
                    AND     A.PART_NUMBER = C.PART_NUMBER
                    AND     B.GROUP_ID = p_group_id
                    AND     F.USER_NAME=B.CREATED_BY_NAME
                    AND     B.ID = TX.ID;

            END IF;
            -- set status of current transaction to imported
            UPDATE xx_vms.es_transaction_data
            SET    status = 2                -- imported
            WHERE CURRENT OF tx_cur;
            <<continue_label>>
            NULL;
            EXCEPTION
                WHEN OTHERS
                THEN
                -- Something went wrong, set status to ERROR
                    DECLARE
                           err VARCHAR2(2048);
                    BEGIN
                           --DBMS_OUTPUT.PUT_LINE( SQLERRM );
                           err := SQLERRM;
                           INSERT INTO XX_VMS.ES_TRANSACTION_ERRORS(SQL_ERROR_MESSAGE,GROUP_ID,es_transaction_id)
                           VALUES(err,p_group_id,tx.id);

                           UPDATE xx_vms.es_transaction_data
                           SET     status = 4                -- Error
                           WHERE CURRENT OF tx_cur;
                    END;
            END;
        END LOOP;
END;

/

执行此存储时,DB会抛出错误:

Compilation errors for PROCEDURE APPS.ES_RELEASE_HOLD_LOT_JOB_DATA
#13#10Error: PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
begin case declare end exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe

Line: 219
Text: EXCEPTION
#13#10Error: PLS-00103: Encountered the symbol "LOOP" when expecting one of the following: ;

Line: 236
Text: END LOOP;

这是什么错误?我怎么能纠正这个? 注意:当我删除EXCEPTION子句时,该过程将编译。

1 个答案:

答案 0 :(得分:1)

你应该遵循BEGIN - EXCEPTION - END结构。您错过了BEGIN关键字。

将BEGIN放在代码的开头,如下所示:

BEGIN
   FOR TX IN tx_cur
   LOOP
      BEGIN <----- this is missing
         v_start_quantity := tx.quantity;
         v_start_date := tx.start_date;