| ---PL/SQL语言部分 --PL.SQL基本格式: --declare --声明部分--一切变量和常量在此声明 --begin -- --主体,执行语句 --end; declare i number(3); begin --给变量赋值 i:=1; dbms_output.put_line('i的值是:'||i); end; --声明常量 declare i constant varchar2(20):='我是摩纳哥鞑子'; begin dbms_output.put_line(i); end; select * from scott.emp; --删除一条记录 declare eno varchar2(5); begin eno:=7369; delete scott.emp where empno=eno; end; --新增一条记录 declare eno varchar2(5):=110; ena varchar2(20):='周星星'; ejob varchar2(30):='影帝'; mgr number(4):=7369; hir date:='3-1月-2011'; sals number(10):=10000; com number(20):=100; dep number:=10; begin insert into scott.emp values(eno,ena,ejob,mgr,hir,sals,com,dep); end; --查询数据 declare eno number(3); ena varchar2(10); begin eno:=110; select ename into ena from scott.emp where empno=eno; dbms_output.put_line('ena的值是:'||ena); end; --显示所有的记录 declare eno varchar2(5):=110; ena varchar2(20); ejob varchar2(30); mgr number(10); hir date; sals number(10); com number(20); dep number(10); begin select empno,ename,job,mgr,hiredate,sal,comm,deptno into eno,ena,ejob,mgr,hir,sals,com,dep from scott.emp where empno=eno; dbms_output.put_line(eno||','||ena||','||ejob||','||mgr||','||hir||','||sals||','||com||','||dep); end; --使用一行简化我们的查询操作 --%rowtype,返回行的数据类型 declare emps scott.emp% rowtype; begin select * into emps from scott.emp where empno=110; dbms_output.put_line(emps.ename||','||emps.job); end; --%type declare enames scott.emp.ename% type; begin select ename into enames from scott.emp where empno=110; dbms_output.put_line(enames); end; --条件控制语言 --if--then--else --if--then--else if--else if --else-- --有多少个if就给多少end if 结束 declare i number:=2; begin if(i>4) then dbms_output.put_line('逻辑正确'); else dbms_output.put_line('逻辑不正确'); endif; end; --查询scott.emp 表中的数据,如果工资低于3000就加2000 --如果低于4000就只加500 --如果高于5000就扣除200 declare emp number(5):=110; esal number(10); begin select sal into esal from scott.emp where empno=emp; if(esal<3000) then update scott.emp set sal=sal+2000; else if(esal>5000) then update scott.emp set sal=sal-200; end if; end if; select sal into esal from scott.emp where empno=emp; dbms_output.put_line(esal); end; --case declare i number(2):=1; begin case i when1 then dbms_output.put_line('i的值是1'); when 2 then dbms_output.put_line('i的值是2'); when 3 then dbms_output.put_line('i的值是3'); else dbms_output.put_line('没有匹配的值'); end case; end; --循环语句 --loop,for.while --简单的loop循环 declare i number(2):=1; begin loop if(i>10) then exit; --终止程序 end if; dbms_output.put_line(i); i:=i+1; end loop; end; --for循环 declare j number(2):=10; begin for i in1..j loop --for循环,不需要声明此处的变量i,范围采用".." dbms_output.put_line(i); end loop; end; --while 循环 declare k number(2):=1; begin while (k<=10) loop dbms_output.put_line(k); k:=k+1; end loop; end; --异常的处理 --预定义异常 --用户自定义异常 declare invalied EXCEPTION; categroy varchar2(10); begin categroy :='tt'; if categroy not in('沈水','林下','石小孟') then raise invalied; else dbms_output.put_line('你是:'||categroy); end if; exception when invalied then dbms_output.put_line('你输入的不匹配'); end; declare j number(2):=10; begin for i in1..j loop --for循环,不需要声明此处的变量i,范围采用".." dbms_output.put_line(i); end loop; end; /* * ** *** **** ***** */ declare begin for i in0..5 loop for j in0..i loop dbms_output.put('*'); end loop; dbms_output.new_line; end loop; end; select * from scott.emp |