• 关于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;

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

    怕什么真理无穷 进一寸有一寸的欢喜
  • 相关阅读:
    #一点杂记
    《洛谷P3373 【模板】线段树 2》
    《Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020
    《牛客练习赛72C》
    《hdu2819》
    《hdu2818》
    《Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)》
    《51nod1237 最大公约数之和 V3》
    对输入的单词进行排序
    快速排序
  • 原文地址:https://www.cnblogs.com/ccbk/p/6493447.html
Copyright © 2020-2023  润新知