PL/SQL的应用操作实例以及解析
declare msg char(15);--定义变量 begin msg:='hell';--赋值 dbms_output.put_line(msg); end;--声明的类型为 与emp.sal类型变量一致declare sal emp.sal%type; --sal和emp表中sal类型一样 begin sal:=900.44; dbms_output.put_line(sal); end; --记录一行declare --声明 emp_rec emp%rowtype;--emp_rec 是一个变量 emp表的一行记录 emp%rowtype begin select * into emp_rec from emp where empno=7499; --给emp_rec 赋值的时候必须给他一条记录 into语句表示赋值 dbms_output.put_line(emp_rec.sal);--不能直接输出emp_rec 需要emp_rec.sal end; --if语句形式1declare grade number(5,2); begin grade:=80; if grade>70 then dbms_output.put_line('greate'); end if; end; --if语句形式2declare grade number(5,2); begin grade:=60; if grade>70 then dbms_output.put_line('greate'); else dbms_output.put_line('bad'); end if; end; --if语句形式3declare grade number(5,2); begin grade:=60; if grade>70 then dbms_output.put_line('greate'); elsif grade>50 then dbms_output.put_line('bad'); else dbms_output.put_line('bad2'); end if; end; accept num prompt '请输入数字';--接收键盘输入的值 会自动保存num的值 declare--声明 pnum number :=#-- &代表取值 pnum 格式 声明变量 变量类型 begin dbms_output.put_line(pnum); end; declare pnum number :=1; begin while pnum<10 --while语句 exit when pnum>10 循环退出的条件 loop dbms_output.put_line(pnum); pnum:=pnum+1;--循环改变的条件 end loop; end; declare begin for i in 1..10 loop dbms_output.put_line(i); end loop; end; select * from emp; declare job_rec emp.job%type; begin select job into job_rec from emp; --存在问题 返回的job岗位为多个,而job_rec只能接受一个值 if job_rec ='MANAGER' then update emp set sal = sal+800; --没有指定where子句 elsif job_rec='SALESMAN' then update emp set sal=sal+400; else update emp set sal = sal+200; end if; end; --声明游标 declare cursor cl is select * from emp;--查询出所有的emp记录存储到cl游标中 emp_rec emp%rowtype; begin open cl;--打开游标 loop exit when cl%notfound; --循环退出的条件 cl%notfound 当没有记录的时候 返回true fetch cl into emp_rec;--检索数据 dbms_output.put_line(emp_rec.empno); end loop; close cl; end; declare emp_row emp%rowtype; begin select * into emp_row from emp; --隐式游标 --例外的声明 exception when too_many_rows then --when 例外的名称 then 发生例外执行的操作 dbms_output.put_line('超了'); end; declare no_data exception; emp_row emp%rowtype; cursor cl(pno number) is select * from emp where empno=pno; begin open cl(0000); fetch cl into emp_row; if cl%notfound then raise no_data; --在这里抛出例外 end if; close cl; exception --在这里捕获例外 when no_data then dbms_output.put_line('没有找到数据'); end; --显示部门号为30的员工信息 declare --查询出所有的emp记录存储到cl游标中 cursor cl(dno number) is select * from dept where deptno=dno; begin for dept_row in cl(30) --打开游标(自动) 检索数据 关闭游标 emp_row 变量也不需要定义 loop dbms_output.put_line('部门名称:'||dept_row.dname||' 员工:'||dept_row.loc); -- dbms_output.put_line(dept_row.loc); end loop; end; --为员工添加工资 declare --定义光标,查询job岗位 和员工号 cursor cr is select empno,job from emp; --声明一个变量 此变量的类型与emp中job类型一致 pjob emp.job%type; epno emp.empno%type; --开始 begin --打开游标 open cr; loop --取出游标 fetch cr into epno,pjob; exit when cr%notfound;--判断游标是否存在 if pjob='CLERK' then update emp set sal=sal+1000 where empno = epno; elsif pjob='MANAGER' then update emp set sal=sal+800 where empno=epno; else update emp set sal=sal+400 where empno=epno; end if; end loop; close cr; end; select * from emp;