--9.用户键盘输入5个数字,将数字按从小到大的顺序存入集合中:declare
declare
type num_type is table of number;
n_tab num_type;
temp_n number ;
begin
n_tab:= num_type(&n1,&n2,&n3,&n4,&n5);
for i in 1..n_tab.count loop
for j in 1..n_tab.count-1 loop
if n_tab(j) > n_tab(j+1) then
temp_n := n_tab(j);
n_tab(j) := n_tab(j+1);
n_tab(j+1):= temp_n;
end if;
end loop;
end loop;
for i in 1..5 loop
dbms_output.put_line(n_tab(i));
end loop;
end;
-- 10.创建一个集合tab1存储5个人名('a','b','c','d','e'),
-- 将名字取出倒序后存回数组tab2('e','d','c','b','a'):
declare
type c_type is table of varchar2(50);
tab1 c_type ;
tab2 c_type := c_type();
x number;
begin
tab1 := c_type('a','b','c','d','e');
x := tab1.count;
for i in 1..tab1.count loop
tab2.extend;
tab2(i) := tab1(x);
x := x-1;
end loop;
for j in 1..tab2.count loop
dbms_output.put_line(tab2(j));
end loop;
end;
--19种方式循环遍历emp表
--1.for循环+子查询
begin
for e in (select * from emp) loop
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.mgr||' '||
to_char(e.hiredate,'yyyy-MM-dd')||' '||e.sal||' '||e.comm||' '||e.deptno);
end loop;
end;
--2.游标+loop
declare
cursor a_cur is select * from emp;
e emp%rowtype;
begin
open a_cur; -- 打开游标
loop
fetch a_cur into e; -- 抓取
exit when a_cur%notfound;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.mgr||' '||
to_char(e.hiredate,'yyyy-MM-dd')||' '||e.sal||' '||e.comm||' '||e.deptno);
end loop;
close a_cur; -- 关闭游标
end;
--3.游标+for
declare
cursor b_cur is select * from emp;
begin
for e in b_cur loop
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.mgr||' '||
to_char(e.hiredate,'yyyy-MM-dd')||' '||e.sal||' '||e.comm||' '||e.deptno);
end loop;
end;
--4.游标+while
declare
cursor c_cur is select * from emp;
e emp%rowtype;
begin
open c_cur;
fetch c_cur into e; -- 进行while循环之前抓取一次做循环条件
while c_cur%found loop
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.mgr||' '||
to_char(e.hiredate,'yyyy-MM-dd')||' '||e.sal||' '||e.comm||' '||e.deptno);
fetch c_cur into e;
end loop;
close c_cur;
end;
--5.游标+goto
declare
cursor a_cur is select * from emp;
e emp%rowtype;
begin
open a_cur; -- 打开游标
<<A>>
fetch a_cur into e; -- 抓取
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.mgr||' '||
to_char(e.hiredate,'yyyy-MM-dd')||' '||e.sal||' '||e.comm||' '||e.deptno);
if a_cur%found then
goto A;
end if;
close a_cur; -- 关闭游标
end;
--6.ref游标+loop
declare
type aRef is ref cursor;
eRef aRef;
e emp%rowtype;
begin
open eRef for select * from emp;
loop
fetch eRef into e;
exit when eRef%notfound;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.mgr||' '||
to_char(e.hiredate,'yyyy-MM-dd')||' '||e.sal||' '||e.comm||' '||e.deptno);
end loop;
close eRef;
end;
--7.ref游标+while
declare
type aRef is ref cursor;
eRef aRef;
e emp%rowtype;
begin
open eRef for select * from emp;
fetch eRef into e;
while eRef%found loop
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.mgr||' '||
to_char(e.hiredate,'yyyy-MM-dd')||' '||e.sal||' '||e.comm||' '||e.deptno);
fetch eRef into e;
end loop;
close eRef;
end;
--8.集合+游标+loop
declare
type e_type is table of emp%rowtype;
cursor e_cur is select * from emp;
eTab e_type := e_type();
e emp%rowtype;
x number := 1;
i number := 1;
begin
open e_cur;
loop
fetch e_cur into e;
exit when e_cur%notfound;
eTab.extend;
eTab(x).empno := e.empno;
eTab(x).ename := e.ename;
eTab(x).job := e.job;
eTab(x).mgr := e.mgr;
eTab(x).hiredate := e.hiredate;
eTab(x).sal := e.sal;
eTab(x).comm := e.comm;
eTab(x).deptno := e.deptno;
x := x+1;
end loop;
close e_cur;
loop
exit when i > eTab.count;
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
i := i+1;
end loop;
end;
--9.集合+游标+while
declare
type e_type is table of emp%rowtype;
cursor e_cur is select * from emp;
eTab e_type := e_type();
e emp%rowtype;
x number := 1;
i number := 1;
begin
open e_cur;
fetch e_cur into e;
while e_cur%found loop
eTab.extend;
eTab(x).empno := e.empno;
eTab(x).ename := e.ename;
eTab(x).job := e.job;
eTab(x).mgr := e.mgr;
eTab(x).hiredate := e.hiredate;
eTab(x).sal := e.sal;
eTab(x).comm := e.comm;
eTab(x).deptno := e.deptno;
x := x+1;
fetch e_cur into e;
end loop;
close e_cur;
while i<=eTab.count loop
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
i := i+1;
end loop;
end;
--10.集合+游标+for
declare
type e_type is table of emp%rowtype;
cursor e_cur is select * from emp;
eTab e_type := e_type();
x number := 1;
begin
for e in e_cur loop
eTab.extend;
eTab(x).empno := e.empno;
eTab(x).ename := e.ename;
eTab(x).job := e.job;
eTab(x).mgr := e.mgr;
eTab(x).hiredate := e.hiredate;
eTab(x).sal := e.sal;
eTab(x).comm := e.comm;
eTab(x).deptno := e.deptno;
x := x+1;
end loop;
for i in 1..eTab.count loop
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
end loop;
end;
--11.集合+游标+goto
declare
type e_type is table of emp%rowtype;
eTab e_type := e_type();
cursor e_cur is select * from emp;
e emp%rowtype;
x number := 1;
begin
open e_cur;
<<A>>
fetch e_cur into e;
if e_cur%found then
eTab.extend;
eTab(x).empno := e.empno;
eTab(x).ename := e.ename;
eTab(x).job := e.job;
eTab(x).mgr := e.mgr;
eTab(x).hiredate := e.hiredate;
eTab(x).sal := e.sal;
eTab(x).comm := e.comm;
eTab(x).deptno := e.deptno;
x := x+1;
goto A;
end if;
close e_cur;
for i in 1..eTab.count loop
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
end loop;
end;
--12.集合+批绑定+loop
declare
type e_type is table of emp%rowtype;
eTab e_type := e_type();
cnt number;
i number := 1;
begin
select count(*) into cnt from emp;
eTab.extend(cnt);
select * bulk collect into eTab from emp;
loop
exit when i > cnt;
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
i := i+1;
end loop;
end;
--13.集合+批绑定+while
declare
type e_type is table of emp%rowtype;
eTab e_type := e_type();
cnt number;
i number := 1;
begin
select count(*) into cnt from emp;
eTab.extend(cnt);
select * bulk collect into eTab from emp ;
while i <= cnt loop
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
i := i+1;
end loop;
end;
--14.集合+批绑定+for
declare
type e_type is table of emp%rowtype;
eTab e_type := e_type();
cnt number;
begin
select count(*) into cnt from emp;
eTab.extend(cnt);
select * bulk collect into eTab from emp;
for i in 1..cnt loop
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
end loop;
end;
--15.集合+批绑定+goto
declare
type e_type is table of emp%rowtype;
eTab e_type := e_type();
cnt number;
i number:=1;
begin
select count(*) into cnt from emp;
eTab.extend(cnt);
select * bulk collect into eTab from emp;
<<A>>
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
if i<cnt then
i := i+1;
goto A;
end if;
end;
-----------方法二(我一开始的做法)
declare
type e_type is table of emp%rowtype;
eTab e_type := e_type();
cnt number;
i number:=1;
begin
select count(*) into cnt from emp;
eTab.extend(cnt);
select * bulk collect into eTab from emp;
loop
if i<=cnt then
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
i := i+1;
else
goto A;
end if;
end loop;
<<A>>
null;-- 占位,不知道为什么<<A>>后面直接跟end; 报错
end;
-----------
--16.游标+集合+批绑定+loop
declare
type e_type is table of emp%rowtype;
eTab e_type := e_type();
cursor e_cur is select * from emp;
i number := 1;
begin
open e_cur;
fetch e_cur bulk collect into eTab;
close e_cur;
loop
exit when i>eTab.count;
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
i := i+1;
end loop;
end;
--17.游标+集合+批绑定+while
declare
type e_type is table of emp%rowtype;
eTab e_Type := e_Type();
cursor eCur is select * from emp;
i number := 1;
begin
open eCur;
fetch eCur bulk collect into eTab;
close eCur;
while i<=eTab.count loop
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
i := i+1;
end loop;
end;
--18.游标+集合+批绑定+for
declare
type e_type is table of emp%rowtype;
eTab e_Type := e_Type();
cursor eCur is select * from emp;
i number := 1;
begin
open eCur;
fetch eCur bulk collect into eTab;
close eCur;
for i in 1..eTab.count loop
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
end loop;
end;
--19.游标+集合+批绑定+goto
declare
type e_type is table of emp%rowtype;
eTab e_type := e_type();
cursor e_cur is select * from emp;
i number := 1;
begin
open e_cur;
fetch e_cur bulk collect into eTab;
close e_cur;
<<A>>
dbms_output.put_line(eTab(i).empno||' '||eTab(i).ename||' '||eTab(i).job||' '||eTab(i).mgr||' '||
to_char(eTab(i).hiredate,'yyyy-MM-dd')||' '||eTab(i).sal||' '||eTab(i).comm||' '||eTab(i).deptno);
i := i+1;
if i<=eTab.count then
goto A;
end if;
end;