PL/SQL编程:
如声明一个记录v_record,它和stu表具有相同的名称和数据类型:
如声明了游标提取sno,sname,sage,则可以使用%rowtype声明一个记录存储相同的信息;
- declare
- cursor cur is select sno,sname,sage from stu;
- cursor_record cur%rowtype;
- begin
- open cur;
- loop
- fetch cur into cursor_record;
- exit when cur%notfound;
- dbms_output.put_line(cursor_record.sno);
- end loop;
- close cur;
- end;
(14)循环结构:
a) loop ...if 条件 then exit ... end loop;
b) loop ... exit when 条件 ... end loop;
- declare
- n number :=1;
- count1 number :=2;
- begin
- loop
- n :=n*count1;
- count1 :=count1+1;
- exit when count1=11;
- end loop;
- dbms_output.put_line(to_char(n));
- end;
c) while 条件 loop ... end loop;
d) for ... in ...loop ...end loop;
- -- case两种用法(1)
- declare
- v_job varchar2(9);
- begin
- select job into v_job from emp where ename = 'KING';
- case v_job
- when 'CLERK' then dbms_output.put_line('KING IS A CLEAK');
- when 'PRESIDENT' then dbms_output.put_line('KING IS A PRESIDENT');
- when 'SALESMAN' then dbms_output.put_line('KING IS A SALESMAN');
- else dbms_output.put_line('KING is not found!');
- end case;
- end;
- --case两种用法(2)
- declare
- v_job varchar2(9);
- begin
- select job into v_job from emp where ename = 'KING';
- case
- when v_job='CLERK' then dbms_output.put_line('KING IS A CLEAK');
- when v_job='PRESIDENT' then dbms_output.put_line('KING IS A PRESIDENT');
- when v_job='SALESMAN' then dbms_output.put_line('KING IS A SALESMAN');
- else dbms_output.put_line('KING is not found!');
- end case;
- end;
- --goto语句的用法
- create table temp (sno char(6),sex char(2));
- declare
- v_counter binary_integer :=1;
- v_sno number(6);
- begin
- v_sno :=10;
- loop
- insert into temp(sno,sex) values (to_char(v_sno),'男');
- v_counter := v_counter + 1;
- v_sno := v_sno +1;
- if v_counter =10 then
- goto loop_end;
- end if;
- end loop;
- <<loop_end>>
- dbms_output.put_line('success');
- end;