转newkid关于plsql精华帖子附自测

newkid大师的精华贴子
http://www.itpub.net/forum.php?mod=forumdisplay&fid=3&filter=typeid&typeid=1808


测试集合是否为空及为null
SQL> declare
  2  type customer_type  is table of varchar2(30);
  3  customer customer_type:=customer_type('customer1','customer2');
  4  begin
  5  if customer is empty then
  6    dbms_output.put_line('customer是空的empty,empty表明已初始化,但是个空集合,没有集合元素');
  7  end if;
  8  
  9  if customer is null then
 10   dbms_output.put_line('customer是null,null表示未初始化');
 11  end if;
 12  
 13  customer.delete;
 14  if customer.count>0 then
 15    dbms_output.put_line('customer已初始化且有集合元素');
 16  end if;
 17  
 18  --用delete方法删除初始化的集合为空再次用is empty判断
 19  if customer is empty then
 20    dbms_output.put_line('customer是空的empty,empty表明已初始化,但是个空集合,没有集合元素');
 21  end if;
 22  
 23  
 24  if customer is null then
 25   dbms_output.put_line('customer是null,null表示未初始化');
 26  end if;
 27  
 28  if customer.count>0 then
 29    dbms_output.put_line('customer已初始化且有集合元素');
 30  else
 31    dbms_output.put_line('customer已初始化但集合元素全被删除了,目前集合元素是0');
 32  end if;
 33  end;
 34  /

customer是空的empty,empty表明已初始化,但是个空集合,没有集合元素
customer已初始化但集合元素全被删除了,目前集合元素是0

PL/SQL procedure successfully completed

小结:
    1,null表示集合未初始化,empty已经初始化,只是集合目前没有元素,
       相当于count=0




   
 函数中传入一个值,向集合尾添加或更新


SQL> create or replace type number_type is table of number;
  2  /


SQL> create or replace function func_test_number
  2  (in_number in number,isreplace in boolean)
  3  return number_type
  4  is
  5  out_number_type number_type:=number_type(1,2,3);
  6  begin
  7  if isreplace then
  8   out_number_type(out_number_type.last):=in_number;
  9  else
 10   out_number_type.extend;
 11   out_number_type(4):=in_number;
 12  end if;
 13  return out_number_type;
 14  end;
 15  /




Function created



SQL> declare
  2  x_number_type number_type;
  3  begin
  4  x_number_type:=func_test_number(4,true);
  5  for i in x_number_type.first..x_number_type.last loop
  6  dbms_output.put_line(x_number_type(i));
  7  end loop;
  8  end;
  9  /

PL/SQL procedure successfully completed

SQL> set serveroutput on
SQL> r

1
2
4

PL/SQL procedure successfully completed



Type created
请使用浏览器的分享功能分享到微信等