转:

在实际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. GeoGebra 代数输入的提示和技巧

    GeoGebra是Mac平台上一款自由且跨平台的动态数学软件,提供各级教育使用,包含了几何.代数.表格.图形.统计和微积分,集中在一个容易使用的软件.   确认表达式 始终按  Enter  键确认您 ...

  2. OneDrive,在云端

    应用场景 1.一份文档下班后还没编辑好,发送到自己的QQ/微信回家后继续编辑: 2.由于来回拷贝同一份文件,导致版本太多,忘记那个是最新版本了: 3.出门在外,客户突然需要一份重要文档,这份文件放在办 ...

  3. spring Boot + MyBatis + Maven 项目,日志开启打印 sql

    在 spring Boot + MyBatis + Maven 项目中,日志开启打印 sql 的最简单方法,就是在文件 application.properties 中新增: logging.leve ...

  4. 推荐系统(recommender systems):预测电影评分--构造推荐系统的一种方法:低秩矩阵分解(low rank matrix factorization)

    如上图中的predicted ratings矩阵可以分解成X与ΘT的乘积,这个叫做低秩矩阵分解. 我们先学习出product的特征参数向量,在实际应用中这些学习出来的参数向量可能比较难以理解,也很难可 ...

  5. Mac下用命令行获取苹果手机的UDID

    在终端输入命令行:system_profiler SPUSBDataType | grep "Serial Number:.*" | sed s#".*Serial Nu ...

  6. [ARC064F] Rotated Palindromes

    题意 给定一个整数N,请你求出有多少字符集为1到K之间整数的字符串,使得该字符串可以由一个长度为N的回文串循环移位后得到.所谓循环移位,就是把字符串的某个前缀(可以为空)移到字符串末尾,如" ...

  7. Django3 的服务器搭建

    进入python虚拟环境 执行以下 命令 source env/bin/active 激活并切换虚拟环境 安装 pip3 install django 创建django项目 django-admin ...

  8. 同余方程组(EXCRT)(luogu4777)

    #include<cstdio> #include<algorithm> #define ll long long using namespace std; ll k; ll ...

  9. while循环 运算符和编码

    昨日回顾 1. 初识python python是一门弱类型的解释型高级编程语言 解释器: CPython 官方提供的默认解释器. c语言实现的 PyPy 把python程序一次性进行编译. IPyth ...

  10. DNN的BP算法Python简单实现

    BP算法是神经网络的基础,也是最重要的部分.由于误差反向传播的过程中,可能会出现梯度消失或者爆炸,所以需要调整损失函数.在LSTM中,通过sigmoid来实现三个门来解决记忆问题,用tensorflo ...