关于orcale创建type的一些小经验(遇到的坑)

  在declare中我一般是这样使用type

   declare
    type test_record is record(
     name  varchar2(100)
    );
    
   type test_table is table of  test_record index by  binary_integer;
   
   test_table1  test_table;
   begin
     
   select 'name1' into  test_table1(1).name from dual;
   select 'name2' into  test_table1(2).name from dual;

   dbms_output.put_line( test_table1(1).name);
   dbms_output.put_line( test_table1(2).name);
   end;

 但是因为要用在这次需求原因,我想将这个自定义的test_table1当做OUT参数在存储过程中返回,就遇到了几个问题

1. 在创建的时候不能用index by  binary_integer ;

2.在使用的时候会出现ORA-06530

之类的问题。最后的解决方式是在这位大神这里找到的

 

 他的博客链接:http://blog.itpub.net/12932950/viewspace-668712/

 

create or replace type typ_info as object
(
  name varchar2(10),
  sex  varchar2(1)
);

create or replace type typ_infos as table of typ_info;

 
declare
  v_infos typ_infos := typ_infos();  --初始化
begin
  v_infos.extend; --扩大空间
  v_infos(1) := typ_info(null, null); --初始化创建的type
  v_infos(1).name := 'lyon';
  v_infos(1).sex := 'M';
 
  v_infos.extend; --扩大空间
  v_infos(2) := typ_info(null, null);--初始化创建的type
  v_infos(2).name := 'lyon';
  v_infos(2).sex := 'M';
end;

 放置在存储过程中就是

create or replace procedure testtype(v_infos out typ_infos) as

begin
  v_infos := typ_infos();
  v_infos.extend; --扩充空间;
  v_infos(1) := typ_info(null, null); --初始化值
  v_infos(1).name := 'name1';
  ---
 --当然在这里你也可以写成
 -- v_infos(1) := typ_info(null, null); --初始化值
 --  v_infos(1).name := 'name1';
 
 -- 将上面两步写成一步
  -- v_infos(1) :=typ_info('name1',null);
  v_infos.extend;
  v_infos(2) := typ_info(null, null);
  v_infos(2).name := 'name';

  for var in 1 ..v_infos.count loop
    dbms_output.put_line(v_infos(var).name);
  end loop;
 
  end testtype;

 搞定睡觉,感谢大神们的博客对我们这些菜鸡的指引,哈哈哈哈谢谢谢谢。

 

转载于:https://www.cnblogs.com/ccbk/p/6493447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值