我的问题是在发出COMMIT之前oracle如何处理INSERT事务。

在执行INSERT事务时,oracle是否会等到在该过程中插入所有记录之后,再发出COMMIT语句时,记录会按顺序保存在该事务中?

在以下代码中,首先进行的插入是行数(元数据),然后光标循环并开始插入实际数据。

是否有可能,在我调用此过程的一个事务中,首先插入我的元数据记录,然后插入一些其他数据(与该事务无关),然后再插入其余数据。因此,循环中的第一个记录和其余记录不会插入到Sequence中。

-- This code belongs to proecdure when ever a user clicks on insert
-- button from the front end form

DECLARE

    rowcnt NUMBER;

    CURSOR c_get_employ IS
    SELECT EMP.EMPLOYER_ID, EMP.EMPLOYER_NAME, EMP.EMPLOYER_LOCATION
          FROM EMP
            WHERE EMP.EMPLOYER_COUNTRY = 'USA'
    ORDER BY EMP.EMPLOYER_ID;

BEGIN

    Select count(*)
    INTO rowcnt
    FROM EMP
    WHERE EMP.EMPLOYER_COUNTRY = 'USA'
    ORDER BY EMP.EMPLOYER_ID;

    -- I want to insert the 'number of employee records' that will be inserted (metadata)

    INSERT INTO EMP_OUTPUT
        (EMPID, EMPNAME, EMPLOC, ECOUNT)
    VALUES
        (,,,rowcnt);

    -- Then loop through the cursor and start inserting the data
    FOR c_post_employ IN c_get_employ LOOP

        INSERT INTO EMP_OUTPUT
            (EMPID, EMPNAME, EMPLOC)
        VALUES
            (c_post_employ.EMPLOYER_ID,c_post_employ.EMPLOYER_NAME,c_post_employ.EMPLOYER_LOCATION);

    END LOOP;

    COMMIT;

END;

最佳答案

如果可能,请使用单个SQL语句。它具有语句级的读取一致性,并且速度更快。

insert into emp_output(empid, empname, emploc, ecount)
with employees as
(
    select employer_id, employee_name, employer_location
    from emp
    where employer_country = 'USA'
    order by employer_id
)
select null, null, null, count(*) from employees
union all
select employer_id, employee_name, employer_location, null from employees;

10-06 06:56