Pl/SQL 自定義型別

紅葉DBA發表於2011-04-12

--PL/SQL記錄型別

declare
type myRecord is record(name emp.ename%type,salary emp.sal%type,work emp.job%type);
myrec1 myRecord;
begin
select ename,sal,job into myrec1 from emp where empno=&no;
dbms_output.put_line('員工姓名:'||myrec1.name||'  工資:'||myrec1.salary||'  職位:'||myrec1.work);
end;

--PL/SQL表型別示例

declare
type myTable is table of emp.ename%type index by binary_integer;
mytab1 myTable;
begin
select ename into mytab1(0) from emp where empno=&no;
dbms_output.put_line('員工姓名是:'||mytab1(0));
end;

--PL/SQL遊標型別變數

declare
type myCursor is ref cursor;
mycur1 myCursor;
v_name emp.ename%type;
v_sal emp.sal%type;
begin
open mycur1 for select ename,sal from emp where deptno=&no;
loop
exit when mycur1%notfound;
fetch mycur1 into v_name,v_sal;
dbms_output.put_line('員工姓名:'||v_name||'  工資:'||v_sal);
end loop;
close mycur1;
end;

--PL/SQL 變長陣列型別;

--定義一個變長陣列;
declare
-- 定義變長陣列型別,設定其最大長度為20 ,型別和emp表中的ename型別一樣;
type ename_array is varray(20) of emp.ename%type;
my_ename ename_array;
begin
my_ename:=ename_array('123','1233','12333');
--讀取簡單的一條記錄,存入變長陣列;
select ename into my_ename(2) from emp where empno=7788;
dbms_output.put_line(my_ename(2));
end;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24465008/viewspace-692333/,如需轉載,請註明出處,否則將追究法律責任。

相關文章