如何在迭代时为oracle游标赋值

时间:2015-09-22 13:36:58

标签: oracle plsql

我有Oracle游标,其中包含员工表记录列表,
其中列值为employee_name,salary,city,salary为null的状态 我希望在光标迭代过程中根据城市或州增加每位员工的工资 例如

if city is X and state is Y salary := 10000   
if city is null and state is Y salary :=2000   
if state is null and city is Z salary :=10001  
This part i don't have any issue

我想在迭代时间填充光标哪个工资 所以一旦我的循环结束,我需要带有填充薪水的光标;
我尝试过很多东西但是无法实现我无法提供任何我尝试过的代码 如果有人可以帮助我,那就太好了 或者它会很棒我可以有新的光标并且可以在迭代第一个时分配值。

我试过的代码

for EMPLOYEE_RECORD in EMPLOYEE_RECORD_CUR (COUNTRY_CODE)
  LOOP
      --update salary based on if else 
      EMPLOYEE_RECORD.SALARY:=45454464646;        

  end loop;

for EMPLOYEE_RECORD in EMPLOYEE_RECORD_CUR (COUNTRY_CODE)
  LOOP

     --checking whether salary has been populated or not 
     -- i know this is wrong approach but need help on it
     dbms_output.put_line( EMPLOYEE_RECORD.SALARY);

  end LOOP;

1 个答案:

答案 0 :(得分:2)

您可以考虑使用oracle集合,在您的情况下,您需要有一个集合(数组)记录。要做到这一点,请参阅ff代码。请在声明部分:

       TYPE myrecordtype is RECORD(employee_name VARCHAR2(32767), 
                                   salary NUMBER, 
                                   city VARCHAR2(32767), 
                                   state VARCHAR2(32767));  
       TYPE collectiontype is table of myrecordtype index by varchar2(32767);
       mycollection collectiontype;

然后在你的循环中添加以下内容:

         mycollection(EMPLOYEE_RECORD.employee_name).salary := --number based on your salary computation;
         mycollection(EMPLOYEE_RECORD.employee_name).city := EMPLOYEE_RECORD.city;
         mycollection(EMPLOYEE_RECORD.employee_name).employee_name := EMPLOYEE_RECORD.employee_name;
         mycollection(EMPLOYEE_RECORD.employee_name).state := EMPLOYEE_RECORD.state;

然后在该循​​环之外,您可以根据员工姓名引用该数组上的薪水(换句话说,您可以根据employee_names“查询”该集合/数组)。它是这样的:

         mycollection(employee_name).salary;

或者如果你想循环整个集合,你可以这样做:

         for EMPLOYEE_RECORD in EMPLOYEE_RECORD_CUR 
          loop
         dbms_output.put_line(mycollection(EMPLOYEE_RECORD.employee_name).salary);
         end loop;