Oracle複雜數據類型

type類型:

  1  declare
  2  v_in_empno emp.empno%type := 7369;
  3  v_out_ename emp.ename%type;
  4  begin
  5  execute immediate 'select ename from emp where empno = '||v_in_empno into v_out_ename;
  6  dbms_output.put_line('僱員名字爲:'||v_out_ename);
  7* end;
SQL> /
僱員名字爲:SMITH

PL/SQL 過程已成功完成。

rowtype類型:

  1  declare
  2  v_emp emp%rowtype;
  3  v_in_empno emp.empno%type := 7369;
  4  begin
  5  execute immediate 'select * from emp where empno = ' ||v_in_empno into v_emp;
  6  dbms_output.put_line('僱員名字爲:'||v_emp.ename||' 薪水爲:'||v_emp.sal);
  7* end;
SQL> /
僱員名字爲:SMITH 薪水爲:8888

PL/SQL 過程已成功完成。

記錄類型:

  1  declare
  2  type emp_record_type is record(
  3  v_ename emp.ename%type,
  4  v_sal emp.sal%type
  5  );
  6  emp_record emp_record_type;
  7  begin
  8  execute immediate 'select ename,sal from emp where empno = 7369' into emp_record;
  9  dbms_output.put_line('僱員名字爲:'||emp_record.v_ename||'  薪水爲:'||emp_record.v_sal);
 10  exception
 11  when no_data_found then
 12  dbms_output.put_line('出現異常');
 13* end;
SQL> /
僱員名字爲:SMITH  薪水爲:8888

PL/SQL 過程已成功完成。

表類型:

  1  declare
  2  cursor v_cursor is select ename from emp where deptno = 10;
  3  type emp_table_type is table of emp.ename%type index by binary_integer;
  4  emp_table emp_table_type;
  5  begin
  6  open v_cursor;
  7  fetch v_cursor bulk collect into emp_table;
  8  for i in 1..emp_table.count() loop
  9  dbms_output.put_line('僱員名字爲:'||emp_table(i));
 10  end loop;
 11  exception
 12  when no_data_found then
 13  dbms_output.put_line('出現異常!');
 14* end;
SQL> /
僱員名字爲:CLARK
僱員名字爲:KING
僱員名字爲:MILLER

PL/SQL 過程已成功完成。

複合類型:

  1  declare
  2  --定義一個記錄類型:emp_record_type
  3  type emp_record_type is record
  4  (
  5  v_empno emp.empno%type,
  6  v_ename emp.ename%type,
  7  v_sal emp.sal%type
  8  );
  9  --聲明emp_record是emp_record_type類型的
 10  emp_record emp_record_type;
 11  --定義一個遊標
 12  cursor emp_cursor is select  empno,ename,sal from emp where deptno = 10;
 13  --定義一個PL/SQL表類型:emp_table_type
 14  type emp_table_type is table of emp_record_type index by binary_integer;
 15  --聲明emp_table是emp_table_type類型的
 16  emp_table emp_table_type;
 17  begin
 18  open emp_cursor;
 19  fetch emp_cursor bulk collect into emp_table;
 20  for i in 1..emp_table.count() loop
 21  dbms_output.put_line('僱員編號爲:'||emp_table(i).empno||'  名字爲:'||emp_table(i).ename||'  ?
 22  end loop;
 23  close emp_cursor;
 24  exception
 25  when no_data_found then
 26  dbms_output.put_line('出現異常');
 27* end;
SQL> /
fetch emp_cursor bulk collect into emp_table;
                                   *
ERROR 位於第 19 行:
ORA-06550: 第 19 行, 第 36 列:
PLS-00597: INTO 列表中的表達式 'EMP_TABLE'  類型錯誤
ORA-06550: 第 19 行, 第 1 列:
PL/SQL: SQL Statement ignored
ORA-06550: 第 21 行, 第 51 列:
PLS-00302: 必須說明 'EMPNO' 組件
ORA-06550: 第 21 行, 第 1 列:
PL/SQL: Statement ignored
 <pre name="code" class="sql">SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
PL/SQL Release 9.2.0.1.0 - Production
CORE    9.2.0.1.0       Production
TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
NLSRTL Version 9.2.0.1.0 - Production


不知道是我的9i版本過低原因還是oracle本身不支持將record和table類型合起來用,找了N久沒找到確切答案,有知道的可以告訴我一下。


歡迎關注行者摩羅微信公衆號(xingzhemoluo),一起交流編程經驗。


發佈了41 篇原創文章 · 獲贊 14 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章