--建立序列

create sequence seq_map_param_id ;

--建立参数表

create table map_param(id number primary key , tname varchar2(30)) ;

--初始化

insert into map_param values (seq_map_param_id.nextval , 'EMP')   ;

insert into map_param values (seq_map_param_id.nextval , 'DEPT') ;

CREATE OR REPLACE PACKAGE BODY utl_map_2 is

PROCEDURE output_map (p_in_tname VARCHAR2,p_in_idx_col_name VARCHAR2 DEFAULT '') IS

tb_count number ;

l_col_type user_tab_cols.DATA_TYPE%type;

main_sql   varchar2(32767);

BEGIN

--各种判断

--表是否存在

select count(*) into tb_count from user_tables where table_name=p_in_tname ;

if tb_count = 0 then

raise_application_error(-20999,'Table not found') ;

end if ;

--列是否存在,map key column类型是否正确

begin

select data_type into l_col_type

from user_tab_cols col

where col.TABLE_NAME=p_in_tname

and col.COLUMN_NAME=p_in_idx_col_name ;

if l_col_type not in ('VARCHAR2') then

raise_application_error(-20999,'idx column only varchar2');

end if ;

exception

when no_data_found then

raise_application_error(-20999,'Column not found in table '||p_in_tname) ;

end ;

--example:declare cursor cur is select * from EMP ; begin for rec in cur loop utl_var_2.EMP_list(rec.ENAME) := rec ; end loop ; end;

main_sql    := 'declare ' ||

'cursor cur is select * from '||p_in_tname||' ; ' ||

'begin ' ||

'for rec in cur loop ' ||

'utl_var_2.'||p_in_tname||'_list(rec.'||p_in_idx_col_name||') := rec ; '||

'end loop ; '||

'end;' ;

execute immediate main_sql ;

--dbms_output.put_line(main_sql) ;

END ;

PROCEDURE var_init  is

pkg_dec_sql varchar2(32767) ;

CURSOR param_cur is select * from map_param ;

begin

pkg_dec_sql := 'create or replace package utl_var_2 is ' ;

for rec in param_cur loop

pkg_dec_sql := pkg_dec_sql || 'type t_'||rec.tname||' is table of '||rec.tname||'%rowtype index by varchar2(4000)  ; '||

rec.tname||'_list t_'||rec.tname||' ; ' ;

end loop ;

/*

example :

create or replace package utl_var_2 is

type t_EMP is table of EMP%rowtype index by varchar2(4000);

EMP_list t_EMP;

type t_DEPT is table of DEPT%rowtype index by varchar2(4000);

DEPT_list t_DEPT;

end;

*/

pkg_dec_sql := pkg_dec_sql || ' end ; ';

execute immediate pkg_dec_sql ;

end var_init ;

END utl_map_2 ;

SQL> begin

2  utl_map_2.output_map(p_in_tname => 'DEPT', p_in_idx_col_name => 'LOC');

3  dbms_output.put_line('deptno : '||utl_var_2.dept_list('NEW YORK').deptno ||' loc : ' ||utl_var_2.dept_list('NEW YORK').loc ) ;

4  end ;

5  /

deptno : 10 loc : NEW YORK

PL/SQL procedure successfully completed

SQL> begin

2  utl_map_2.output_map(p_in_tname => 'EMP', p_in_idx_col_name => 'ENAME');

3  dbms_output.put_line('ename : '||utl_var_2.emp_list('SMITH').ename ||' job : ' ||utl_var_2.emp_list('SMITH').job ) ;

4  end ;

5  /

ename : SMITH job : CLERK

PL/SQL procedure successfully completed

不过每次配置后的时候需要手动重新执行以下初始化程序 utl_map_2.var_init

oracle 实现map,PLSQL实现的map功能,以及一些疑惑相关推荐

  1. Map的引入以及Map的功能

    * 需求:查询学生的学号,学号---对应一个学生的姓名  * Java提供了Map<K,V>:键映射到值,一个键对应一个值  * Map集合针对键有效,跟值无关(键必须唯一,键不能重复) ...

  2. 记录:谷歌地图google map api实现基本测距功能

    测距 demo-代码 <!DOCTYPE html> <eteral:html><head><meta name="viewport" c ...

  3. java map 实例_java中map集合嵌套形式简单示例

    定义了一个学生类,封装了id和name属性,提供一个全参构造器,并复写toSting方法 class Student{ private String id; private String name; ...

  4. map函数作用c语言,c语言中map的用法:map基本用法

    c++中map容器提供一个键值对容器,那么你知道map的用法有哪些吗,下面秋天网 Qiutian.ZqNF.Com小编就跟你们详细介绍下c语言中map的用法,希望对你们有用. c语言中map的用法:m ...

  5. mysql 映射到map null_mybatis处理查询map列表属性为null的问题,而导致查询map无该key对象...

    1.常规处理方法(数据库以mysql为例) IFNULL(m.last_use_time,) ) ) as last_lat if判断是否为null,设置一个默认值. 2.前台jsp页面处理,判断是否 ...

  6. python里map函数_python中map()函数的用法讲解

    原博文 2018-10-26 12:59 − map函数的原型是map(function, iterable, -),它的返回结果是一个列表. 参数function传的是一个函数名,可以是python ...

  7. map multimapc++_C++的Map和Multimap

    广州C++培训的小编这一期给大家讲Map和Multimap. 6.6 Maps和Multimaps map和multimap将key/value pair当作元素进行管理.他们可根据key的排序准则自 ...

  8. Map获取键值,Map的几种遍历方法

    2019独角兽企业重金招聘Python工程师标准>>> Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry ...

  9. 【Groovy】map 集合 ( map 集合遍历 | 使用 map 集合的 find 方法遍历 map 集合 | 代码示例 )

    文章目录 一.使用 map 集合的 find 方法遍历 map 集合 二.代码示例 一.使用 map 集合的 find 方法遍历 map 集合 使用 map 集合的 find 方法遍历 map 集合 ...

最新文章

  1. Visual Studio 2005 Web Application Projects 正式推出
  2. golang 中的sort 包
  3. Qt Creator设置3D组件属性
  4. 4月18日 MySQL学习
  5. SQL点滴19—T-SQL中的透视和逆透视
  6. php跳转分站,PHP判断IP并转跳到相应城市分站的方法
  7. python保存rtmp流_ffmpeg 推送、保存rtmp 流命令
  8. C#将Access数据库导出为JSON
  9. 独家 | 一文读懂Adaboost
  10. 如何衡量多元线性回归模型优劣
  11. 关于Activity跳转动画大汇总
  12. DB2 SQLCODE 异常大全编辑(五)
  13. 计算机的硬盘和光驱的接口是什么类型的接口,连接硬盘和光驱是什么接口
  14. 《修C传》——初始C语言 <凝气篇>
  15. 密度测量:1.密度测量的基础知识
  16. python全局代理_Python3 中代理使用方法总结
  17. OpenCV基础入门【C++及python语言】
  18. 实战ssl-bump,实现squid的url过滤功能
  19. 2021年中国外汇交易情况分析:中国银行结汇金额为16.5万亿元,同比增长17%[图]
  20. PyQT5 (四十六) 在 QTableWidget 表格中设置合并单元格 的案例

热门文章

  1. git 入门教程之协同开发
  2. mysql转oracle注意事项
  3. 过滤器解决Struts2重定向漏洞
  4. SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传
  5. Deactivate .NET refector
  6. FSMC(STM32)
  7. SQL Server 2012中的ColumnStore Index尝试
  8. MySQL配置文件my.cnf中文版
  9. 设备驱动--中断开关执行的匹配
  10. 论文被拒怎么办?(下)