转:

在实际PL/SQL编程中,我们要对动态取出来的一组数据,进行For循环处理,其基本程序逻辑为:

1
2
3
4
5
6
7
8
9
10
11
12
create or replace procedure getidlist
is
  l_idlist varchar2(200);
begin
  l_idlist:='1,2,3,4';
  for brrs in (select * from bldroom where bldroomid in (l_idlist))
  loop
      brrs.structure:='钢混';
  end loop;
end;
/
show err;

1、编译该程序,可以正常通过;

2、执行该程序exec getidlist,系统提示:ORA-01722: invalid number,ORA-06512: at "TT.GETIDLIST", line 6

解决方案:

1
CREATE OR REPLACE TYPE type_split IS TABLE OF VARCHAR2 (4000);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
create or replace function split(p_list varchar2,p_sep varchar2 := ',') return type_split pipelined
IS
 l_idx pls_integer;
 v_list varchar2(50) := p_list;
 begin
      loop
           l_idx := instr(v_list,p_sep);
           if l_idx > 0 then
               pipe row(substr(v_list,1,l_idx-1));
               v_list := substr(v_list,l_idx+length(p_sep));
           else
                pipe row(v_list);
                exit;
           end if;
      end loop;
      return;
 end split;

此时修改getidlist代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
create or replace procedure getidlist
is
  l_idlist varchar2(200);
begin
  l_idlist:='1,2,3,4';
  for brrs in (select * from bldroom where bldroomid in (select column_value from table(split(l_idlist,','))))
  loop
      brrs.structure:='钢混';
  end loop;
end;
/
show err;

执行:exec getidlist;

提示错误:ORA-22905: cannot access rows from a non-nested table item

再次修改getidlist代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
create or replace procedure getidlist
is
  brRow    bldroom%rowtype;
  l_idlist varchar2(200);
begin
  l_idlist:='1,2,3,4';
  for idrs in (select column_value from table(split(l_idlist,',')))
  loop
      select * into brRow from bldroom where bldroomid=idrs.column_value;
      brRow.structure:='ssss';
  end loop;
end;
/
show err;

OK,搞定。

附:PL/SQL表---table()函数用法

摘录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
利用table()函数,我们可以将PL/SQL返回的结果集代替table。
oracle内存表在查询和报表的时候用的比较多,它的速度相对物理表要快几十倍。
*/
/*
simple example:
1、table()结合数组:
*/
 
create or replace type t_test as object(
id integer,
rq date,
mc varchar2(60)
);
 
create or replace type t_test_table as table of t_test;
 
create or replace function f_test_array(n in number default null) return t_test_table
as
v_test t_test_table := t_test_table();
begin
for i in 1 .. nvl(n,100) loop
v_test.extend();
v_test(v_test.count) := t_test(i,sysdate,'mc'||i);
end loop;
return v_test;
end f_test_array;
/
 
select * from table(f_test_array(10));
 
select * from the(select f_test_array(10) from dual);
 
/*
2、table()结合PIPELINED函数:
*/
 
create or replace function f_test_pipe(n in number default null) return t_test_table PIPELINED
as
v_test t_test_table := t_test_table();
begin
for i in 1 .. nvl(n,100) loop
pipe row(t_test(i,sysdate,'mc'||i));
end loop;
return;
end f_test_pipe;
/
 
select * from table(f_test_pipe(20));
 
select * from the(select f_test_pipe(20) from dual);
 
/*
3、table()结合系统包:
*/
 
create table test (id varchar2(20));
insert into test values('1');
commit;
explain plan for select * from test;
select * from table(dbms_xplan.display);

关于错误:ORA-22905: cannot access rows from a non-nested table item 的解决方案,不知各位大侠有没有更好的解决方案?请指教。

Oracle中使用Table()函数解决For循环中不写成 in (l_idlist)形式的问题的更多相关文章

  1. js循环函数中的匿名函数和闭包问题(匿名函数要用循环中变量的问题)

    js循环函数中的匿名函数和闭包问题(匿名函数要用循环中变量的问题) 一.总结 需要好好看下面代码 本质是因为匿名函数用到了循环中的变量,而普通方式访问的话,匿名函数的访问在循环之后,所以得到的i是循环 ...

  2. 对JSON.parse()中存在转义字符的解决以及js中替换函数replace()的认识

    在工作中,遇到对页面数据进行转存json格式数据后存储在数据库中.然而在显示数据时遇到无法显示json中的数据,产生的bug 问题抛出: 1.首先认识下,在JSON.parse()将后台传过来的字符串 ...

  3. VUE项目中使用this.$forceUpdate();解决页面v-for中修改item属性值后页面v-if不改变的问题

    VUE项目中使用this.$forceUpdate();解决页面v-for中修改item属性值后页面v-if不改变的问题:https://blog.csdn.net/jerrica/article/d ...

  4. Lua中的table函数库

    table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...

  5. 解决 java循环中使用 Map时 在put值时value值被覆盖的问题

    其实很简单,只需要把容器换成list 然后在循环中,每次循环末尾map = new HashMap() 或者直接在循环中一开始就实例化hashmap(Map map = new HashMap();) ...

  6. python中的enumerate函数用于遍历序列中的元素以及它们的下标

    enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a1 b2 c ...

  7. 用闭包解决 js 循环中函数变量暂存问题

    需求:有一个数组,根据数组的值渲染对应的数字div,单击对应的div 在控制台打印对应的数字.如点击1,控制台打印1. 问题: 不管点击哪个值 打出来都是4 代码如下 <!DOCTYPE htm ...

  8. 解决for循环中空格的问题

    [root@node-01 ~]# cat 1 a b c ab cd 如果想按行循环遍历出文件中内容,直接使用for是有问题的,第一行按空格分隔的会有问题 [root@node-01 ~]# for ...

  9. IntelliJ中的main函数、for循环、System.out.println()快捷键

    main函数 输入: psvm 回车 输出: public static void main(String[] args) { } for循环 输入:fori 回车 输出: for (int i = ...

随机推荐

  1. linux下apache和tomcat整合

    一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...

  2. 【Spring Boot】Spring Boot之使用Alibaba Cloud Toolkit(Idea插件)本地一键部署Spring Boot项目到远程服务器

    一.Alibaba Cloud Toolkit(Idea插件)的安装 1)Alibaba Cloud Toolkit 介绍 Cloud Toolkit 是本地 IDE 插件,帮助开发者更高效地开发.测 ...

  3. 树莓派3b+更改静态IP

    ubuntu系统修改静态IP的方法是在修改/etc/network/interfaces文件,而树莓派此文件下有说明: # interfaces() ) and ifdown() # Please n ...

  4. 折腾deepin修改终端语言

    原创作品,作者是博客园sogeisetsu,转载请注明来源sogeisetsu.cnblogs.com 唉-都怪当初没学扎实,改个终端语言花费了半天. 首先,介绍一下我的情况 有两个用户,一个是roo ...

  5. PAT 乙级 1009.说反话 C++/Java

    1009 说反话 (20 分) 题目来源 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词 ...

  6. 常用 shell 命令 chmod | root

    chmod 命令 chmod 命令 [格式1:] chmod [ugoa][+-=][rwx] 文件或目录 /*u.g.o.a : u属主,g属组,o其他用户,a所有用户*/ /*+.-.= : 增加 ...

  7. c语言位域的使用注意事项——数据溢出

    c语言可以使用位域来节省变量的空间,例如开关只有通电和断电两种状态,用 0 和 1 表示足以,也就是用一个二进位.位域的取值范围非常有限,数据稍微大些就会发生溢出,这个字使用keil的使用,keil提 ...

  8. 数据库plsql配置

    https://jingyan.baidu.com/article/d8072ac4957b28ec95cefd9f.html

  9. 小程序&app 注册登录、绑定

    前段时间开发中的一款产品,有小程序和app:小程序直接微信登录,app使用手机号+验证码注册,手机号+验证码/密码登录. 用户使用其中一套账号密码即可正常使用,不强制要求完善另一套账号.为避免同一用户 ...

  10. DIP大作业---图像分割

    数字图像处理课程的大作业,要求如下: 图像分割就是把图像分成若干个特定的.具有独特性质的区域并提出感兴趣目标的技术和过程.它是由图像处理到图像分析的关键步骤.现有的图像分割方法主要分以下几类:基于阈值 ...