转:

在实际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. Django之DRF源码分析(二)---数据校验部分

    Django之DRF源码分析(二)---数据校验部分 is_valid() 源码 def is_valid(self, raise_exception=False): assert not hasat ...

  2. “IOS11不再信赖WOSIGN证书”公众号运营者如何应对

    ptd->_thandle = (uintptr_t)(-1); {{}/*** 传入需要的参数,设置给*/{}给Fragment添加newInstance方法,将需要的参数传入,设置到bund ...

  3. 运行 npm run lint -- --fix,提示:error Use the global form of 'use strict'

    运行 npm run lint -- --fix,提示:error Use the global form of 'use strict',使用说明网址:https://eslint.org/docs ...

  4. woocommerce调用产品相册gallery图片如何操作?wordpress技巧

    wordpress官网有很多woocommerce模板,但有些客户要求定制模板,这时可能会碰到产品相册图片调用的问题,如果根据自带的Storefront主题来改很麻烦,那我们就自己定义吧!下来就随yt ...

  5. linux定时任务(转)

    转自:https://www.cnblogs.com/intval/p/5763929.html linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非 ...

  6. 各位大神,我请教一个问题,我在Android studio上创一个project显示错误

    Error:FAILURE: Build failed with an exception. * Where: Build file 'C:\Users\Administrator\AndroidSt ...

  7. spring:过滤器和拦截器

    过滤器:网络通信模型的会话层控制: 拦截器:事务处理的aop注入(生命周期监控). 对于Servlet Filter,官方文档中说的很好, 并且给出了常见的应用场景. A filter is an o ...

  8. Trolley slow

  9. OKR失败的五个关键因素

    OKR是近年来的一个热点话题,这种目标管理法在谷歌体现了它非凡的价值,也因此被Facebook.Linkedin等公司所引用.从实践成功的案例看来,OKR确实是一种可以明确公司目标.促进公司发展的有价 ...

  10. kafk设计要点

    kafka的设计目标是高吞吐量,所以kafka自己设计了一套高性能但是不通用的协议,他是仿照AMQP( Advanced Message Queuing Protocol   高级消息队列协议)设计的 ...