暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 将集合值动态分配给变量

askTom 2017-03-02
465

问题描述

emp表
创建表emp(empno number,ename,varchar2(30),sal number deptno number);
部门表
创建表dept(deptno number,deptname,varchar2(30);

声明
类型ty_test(emp emp % rowtype,dept % rowtype);
ty_table类型是ty_test的表;
tbl_record ty_table:= ty_table();
v_table1:= '从empy中选择 *';
v_table2:= '从dep' 中选择 *;
I号: = 1;
val varchar2(300);
c1系统 _ 参考光标;
开始
为v_table1打开c1;
将C1取入tbl_record(I).p_emp;
当C1% 未找到时退出;
I:= i 1;
结束循环;
关闭C1;
i:= 1;
为v_table2打开c1;
将C1取入tbl_record(I).p_dept;
当C1% 未找到时退出;
I:= i 1;
结束循环;
关闭C1;
/*
我的要求是我将获得一个数字,表名和column_name的参数,我应该从集合tbl_record中获取该值
说参数为no: 1,table_name = 'EMP' COLUMN_NAME = 'ENAME'
如何动态获取tbl_record的值 (否).table_name.COLUMN_NAME
如果我硬编码tbl_record(1).emp.ENAME并分配给变量,则可以获取它 (val:= 硬编码tbl_record(1).emp.ENAME)。。如何根据参数动态执行此操作
*/
结束;

专家解答

我不确定我是否遵循您的代码-当然构造无效

SQL> declare
  2  type ty_test(emp scott.emp%rowtype,dept scott.dept%rowtype);
  3  begin
  4    null;
  5  end;
  6  /
type ty_test(emp scott.emp%rowtype,dept scott.dept%rowtype);
            *
ERROR at line 2:
ORA-06550: line 2, column 13:
PLS-00103: Encountered the symbol "(" when expecting one of the following:
; is authid as force under accessible
ORA-06550: line 3, column 1:
PLS-00103: Encountered the symbol "BEGIN"
ORA-06550: line 5, column 4:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
复制


如果要同时返回EMP和DEPT数据,则将2个集合返回给调用环境。但是,如果您真的说这些只是示例,并且我们可能会传递任意表列表,那么我们将进入动态SQL领域。如果直到运行时才知道从游标返回的列的数量和/或名称,则这将需要动态SQL。下面是一个脚本示例,该脚本演示了DBMS_SQL中的各种工具,以查看

-查询中有哪些列
-它们是什么数据类型
-如何迭代获取它们

SQL> set serverout on
SQL> declare
  2      l_query varchar2(32767) := 'select * from scott.dept';
  3
  4      l_theCursor     integer default dbms_sql.open_cursor;
  5      l_columnValue   varchar2(4000);
  6      l_status        integer;
  7      l_descTbl       dbms_sql.desc_tab;
  8      l_colCnt        number;
  9      n number := 0;
 10    procedure p(msg varchar2) is
 11      l varchar2(4000) := msg;
 12    begin
 13      while length(l) > 0 loop
 14        dbms_output.put_line(substr(l,1,80));
 15        l := substr(l,81);
 16      end loop;
 17    end;
 18  begin
 19      dbms_sql.parse(  l_theCursor,  l_query, dbms_sql.native );
 20      --
 21      -- how to get the columns from the query
 22      --
 23      dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );
 24
 25      --
 26      -- how to define return values from the cursor once we fetch from it
 27      --
 28      for i in 1 .. l_colCnt loop
 29          dbms_sql.define_column(l_theCursor, i, l_columnValue, 4000);
 30      end loop;
 31
 32      l_status := dbms_sql.execute(l_theCursor);
 33
 34      --
 35      -- how to fetch each column and each row
 36      --
 37      while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
 38          for i in 1 .. l_colCnt loop
 39              dbms_sql.column_value( l_theCursor, i, l_columnValue );
 40              p( rpad( l_descTbl(i).col_name, 30 )
 41                || ': ' ||
 42                l_columnValue );
 43          end loop;
 44          dbms_output.put_line( '-----------------' );
 45          n := n + 1;
 46      end loop;
 47      if n = 0 then
 48        dbms_output.put_line( chr(10)||'No data found '||chr(10) );
 49      end if;
 50  end;
 51  /
DEPTNO                        : 10
DNAME                         : ACCOUNTING
LOC                           : NEW YORK
-----------------
DEPTNO                        : 20
DNAME                         : RESEARCH
LOC                           : DALLAS
-----------------
DEPTNO                        : 30
DNAME                         : SALES
LOC                           : CHICAGO
-----------------
DEPTNO                        : 40
DNAME                         : OPERATIONS
LOC                           : BOSTON
-----------------
复制


「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论