正确的操作: declare pagesize constant number(2) :=10; begin dbms_output.put_line(pagesize); end; /
作用域: 是能够引用变量名称这样的标识符的程序块。
declare pagesize number(2):=10; //在整个中都有效 begin dbms_output.put_line(pagesize); declare pagesize number(2):=99; //只是在内容声明部分到end结束有效 begin dbms_output.put_line(pagesize); end; end; /
//声明一个整型的变量 SQL> declare 2 age number(2); 3 begin 4 age:=90; 5 dbms_output.put_line(age); 6 end; 7 / //声明后直接赋值 SQL> declare 2 age number(2) :=90; 3 begin 4 dbms_output.put_line(age); 5 end; 6 / //捕获异常处理 SQL> declare 2 age number(2); 3 begin 4 age :='qwe' 5 ; 6 exception 7 when others then 8 dbms_output.put_line('赋值失败'); 9 end; 10 /
条件判断语句 有if与case语句
If语句 If语法 If(expression1)then pl/sql_statement Else Pl/sql_statement; End if;
If(expression1)then pl/sql_statement Else if(expression2) Pl/sql_statement; Else Pl/sql_statement; End if;
Case语句 从oracle9i以后引入 Case <selector> When <expression1> then pl/sql_statement1; When <expression2> then pl/sql_statement2; ……. [else pl/sql_statement;] End; //案例 SQL> declare score number(2) :=80; begin if score>70 then dbms_output.put_line('成绩合格'); end if; end; /
SQL> declare score number(2) :=80; begin if score>90 then dbms_output.put_line('成绩合格'); else dbms_output.put_line('成绩不合格'); end if; end; /
成绩不合格
SQL> declare score number(2):=8; begin case score when 9 then dbms_output.put_line('成绩优秀'); when 8 then dbms_output.put_line('成绩亮'); end case; end; /
循环语句 最基本的循环称为无条件循环,如果没有指定exit语句,循环将无条件执行,这种循环称为死循环,死循环尽量避免。 语法格式如下: Loop ---statement--- Exit when condition End loop;
案例: SQL> declare i number(2):=1; begin loop dbms_output.put_line(i); i:=i+1; exit when i>10; end loop; end; /
While循环 语法: While condition Loop Statement; End loop;
SQL> declare i number(2):=1; begin while i<10 loop dbms_output.put_line(i); i:=i+1; end loop; end; /
For循环 For loop_control_variable in [reverse] lower upper loop Statement; End loop;
SQL> begin for i in 1..10 loop dbms_output.put_line(i); end loop; end; /
PL/SQL语句中的SQL语句 declare countnum number(2); //声明变量 begin select count(*) into countnum from emp; //into的作用把从数据中检测出的数据赋值给哪个变量 dbms_output.put_line(countnum); end; / 注意:如果在select中没有into语句会出现bug
declare empno number(4); ename varchar2(10); job varchar2(9); begin select empno,ename,job into empno,ename,job from emp; dbms_output.put_line(empno||'/'||ename||'/'||job); end; / 以上代码中会出现bug bug原因是因为定义的变量只可以存储一个单独的值,而你返回的是多个值.所以应该为如下: SQL> declare 2 empno number(4); 3 ename varchar2(10); 4 job varchar2(9); 5 begin 6 select empno,ename,job into empno,ename,job from emp where empno='7369'; //在这加上where条件了 7 dbms_output.put_line(empno||'/'||ename||'/'||job); 8 end; 9 /
2、 定义记录表类型 TYPE table_name is table of data_type[not null] Index by binary_integer;//主键的索引 declare type table_emp is table of emp%rowtype //创建一个表 此表的类型与emp表的类型一致 index by binary_integer;
type table_text is table of varchar2(20) //创建一个表 此表具有一个varchar2列的简单表 index by binary_integer;
empt table_emp; //声明记录表类型的变量 tabtext table_text; begin
案例: SQL> declare 2 type table_emp is table of emp%rowtype 3 index by binary_integer; 4 empt table_emp; 5 begin 6 empt(1).ename:='wangyi'; 7 dbms_output.put_line(empt(1).ename); 8 end; 9 /
//返回总记录 SQL> declare 2 type table_emp is table of emp%rowtype 3 index by binary_integer; 4 empt table_emp; 5 begin 6 dbms_output.put_line(empt.count); 7 end; 8 /
0 //没有记录
//删除的操作 表名.Delete(记录数);
//检索记录变量 First:获取第一个的索引 Next:下一个的索引 但是必须有参数 Last:最后一个的索引 SQL> declare 2 type table_emp is table of emp%rowtype 3 index by binary_integer; 4 empt table_emp; 5 i number(2):=1; 6 begin 7 while i<10 8 loop 9 empt(i).ename:='wangyi'; 10 i:=i+1; 11 end loop; 12 13 dbms_output.put_line(empt.count); 14 15 empt.delete(2); 16 17 dbms_output.put_line(empt.count); 18 19 dbms_output.put_line(empt.first); 20 dbms_output.put_line(empt.next(2)); 21 dbms_output.put_line(empt.last); 22 end; 23 /
9 8 1 3 9
//查询数据库的数据赋值给你创建的记录表类型 分析一: SQL> declare 2 type table_emp is table of emp%rowtype 3 index by binary_integer; 4 empt table_emp; 5 i number(10):=0; 6 begin 7 8 for rec in (select * from emp) loop 9 i:=i+1; 10 empt(i).ename:=rec.ename; 11 end loop; 12 13 dbms_output.put_line(empt.count); 14 end; 15 /
15 具体实现:
//把查询的的数据全部赋值给记录表类型 declare type table_emp is table of emp%rowtype index by binary_integer; empt table_emp; i number(10):=0; j number(10):=1; begin
for rec in (select * from emp) loop i:=i+1; empt(i).empno:=rec.empno; empt(i).ename:=rec.ename; empt(i).job:=rec.job; empt(i).mgr:=rec.mgr; empt(i).hiredate:=rec.hiredate; empt(i).sal:=rec.sal; empt(i).comm:=rec.comm; empt(i).deptno:=rec.deptno; end loop;
while j<=empt.count loop dbms_output.put_line(empt(j).empno||'/'||empt(j).ename||'/'||empt (j).job||'/'||empt(j).mgr||'/'||empt(j).hiredate||'/'||empt(j).sal||'/'||empt (j).comm||'/'||empt(j).deptno); j:=j+1; end loop; end; /
SQL> declare 2 type table_emp is table of emp%rowtype 3 index by binary_integer; 4 empt table_emp; 5 i number(10):=0; 6 j number(10):=1; 7 begin 8 9 for rec in (select * from emp) loop 10 i:=i+1; 11 empt(i).empno:=rec.empno; 12 empt(i).ename:=rec.ename; 13 empt(i).job:=rec.job; 14 empt(i).mgr:=rec.mgr; 15 empt(i).hiredate:=rec.hiredate; 16 empt(i).sal:=rec.sal; 17 empt(i).comm:=rec.comm; 18 empt(i).deptno:=rec.deptno; 19 end loop; 20 21 dbms_output.put_line(empt.count); 22 23 while j<=empt.count loop 24 dbms_output.put_line(empt(j).empno||'/'||empt(j).ename||'/'||empt 25 26 (j).job||'/'||empt(j).mgr||'/'||empt(j).hiredate||'/'||empt(j).sal||'/'||empt 27 28 (j).comm||'/'||empt(j).deptno); 29 j:=j+1; 30 end loop; 31 end; 32 /