How to store selection result in to variable in Oracle procedure

前端 未结 2 1775
失恋的感觉
失恋的感觉 2021-02-08 22:11

I write a simple procedure. I try to store selection result in variable. I use \"SELECT INTO\" query but I can not doing this.

Example:

DECLARE
     v_em         


        
2条回答
  •  清酒与你
    2021-02-08 22:36

    You have a couple options. You could turn that query into a cursor:

    DECLARE
         CURSOR v_employeeRecords IS
              SELECT * FROM Employee WHERE Salary > 10;
         v_employeeRecord  employee%ROWTYPE;
    BEGIN
         FOR v_employeeRecord IN v_employeeRecords LOOP
              /* Do something with v_employeeRecord */
         END LOOP;
    END;
    

    Or, you can create a TABLE variable:

    DECLARE
         v_employeeRecord  employee%ROWTYPE;
         v_employeeRecords IS TABLE OF employee%ROWTYPE;
         i BINARY_INTEGER;
    BEGIN
     SELECT * BULK COLLECT INTO v_employeeRecords
          FROM Employee WHERE Salary > 10;
    
     i := v_employeeRecords.FIRST;
     WHILE v_employeeRecords.EXISTS(i) LOOP
         v_employeeRecord := v_employeeRecords(i);
         /* Do something with v_employeeRecord */
         i := v_employeeRecords.NEXT(i);
     END;
    END;
    

    I haven't tried these samples in Oracle, so you may get compiler errors...

提交回复
热议问题