转:

在实际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. spring data jpa 表关联设置用户表关联角色表配置

    User 表: @ManyToMany(cascade = { CascadeType.MERGE }) @JsonIgnore @JoinTable(name = "UserRole&qu ...

  2. nginx.conf配置项

    环境:centos7  nginx1.16.1 以下配置均在配置文件中进行:/etc/nginx/nginx.conf 1.设置工作进程的所有者和所属组 user  所有者  所属组: 设置后要在操作 ...

  3. centos7搭建lnmp

    一.准备 1. 修改网络yum源 先将系统自带的yum配置文件重命名或删除,然后下载下面两个文件 阿里云:http://mirrors.aliyun.com/repo/Centos-7.repo ep ...

  4. 百度云人脸识别API人脸库管理

      from urllib import request import base64 import requests import re import json import urllib impor ...

  5. django项目中使用邮箱找回密码功能

    本文使用qq邮箱,需要登录邮箱,在设置-账户里面开启SMTP服务,要记下授权码 前端html {#找回密码的表单#} <form action="" method=" ...

  6. c+多态的本质:编译器维护了类型信息同时插入了解释执行机制

    Calling a virtual function is slower than calling a non-virtual function for a couple of reasons: Fi ...

  7. mysql关键字--设计表时要避开,否则回报语法错误

    https://dev.mysql.com/doc/refman/5.7/en/keywords.html#keywords-in-current-series Keywords and Reserv ...

  8. HUSTOJ 有序表的最小和

    一次奇怪的AC经历...上周被这道题卡了3天... 传送门:http://oj.gdsyzx.edu.cn/problem.php?id=1475 题目描述 给出两个长度为n的有序表A和B,在A和B中 ...

  9. UFUN函数UF_MODL UF_DISP UF_OBJ(name_switch) ( UF_DISP_ask_name_display_status、UF_DISP_set_name_display_status)

    /* TODO: Add your application code here */ /* 此程序主要演示的是name_switch (设置名称显示) */ UF_initialize(); //MO ...

  10. PHP7新增的主要特性

    1.use的用法 <?php // PHP 7 之前版本用法 use some\namespace\ClassA; use some\namespace\ClassB; use some\nam ...