我需要在plsql中使用游标比较集合

时间:2013-12-05 04:52:20

标签: oracle plsql

declare

    cursor c1 is
    select last_name ls  from empc;

    type x is table of employees.last_name%type;
    x1 x := x();
    cnt integer :=0;

begin

    for z in c1 loop

        cnt := cnt +1;
        x1.extend;
        x1(cnt) := z.ls;

        if x1(cnt) = 'NULL' then
            DBMS_OUTPUT.PUT_LINE('---');
        end if;
        dbms_output.put_line(cnt || ' '|| x1(cnt));

    end loop;

end;

在这段代码中,我需要在这种情况下用一些值替换空值(------)。但我无法找到解决方案。

1 个答案:

答案 0 :(得分:1)

假设您希望X1具有与空值不同的值。

您可以在查询(光标)中使用NVL功能,如下所示:

cursor c1 is
select nvl(last_name, '----') ls  from empc;

但如果这是你想要做的所有事情,那么你可以让整个事情更简单:

declare

    type x is table of employees.last_name%type;
    x1 x := x();

begin

    select nvl(last_name, '---') ls bulk collect into x1 from empc;

    -- This is just for printing the results
    for cnt in x1.first .. x1.last loop

        dbms_output.put_line(cnt || ' '|| x1(cnt));

    end loop;

end;
相关问题