基础的PL/SQL(DML)select,update,insert,delete语句:
1.select语句
select 要查的东西 into 指定的变量 from 表名 where 条件;
select ename,sal,age into v_ename, v_sal,v_age from emp where deptno='&no';
2.insert语句
插入数据可以用values语句,也可以用select子查询。 insert 后面的指出的字段的数据类型和个数要与子查询中的字段的数据类型和个数完全匹配。
insert into 表名(表中的属性1,属性2...) values (值1,值1)/(变量1,变量2);
insert into emp(ename,sal) values(shu,8000);
3.update语句
列的新值必须满足该列的数据类型,长度及约束条件。
(1).update 表名 set 表中的属性=新值 where 条件;
update emp set ename='澈' where empno='&empno';
(2).带有子查询的更新:update 表名 set(表中的属性1,属性2)=(select 属性1,属性2 from 表名 where 条件) where jop=(select jop from 表名 where 条件);
update emp set (sal,age)=(select sal,age from emp where empno=v_empno)
where ename=(select ename from emp where empno=v_empno);
where 后面可不可以去掉,不想要。
4.delete语句(跟之前的一样)
(1).直接删除表中的所有数据:
delete emp where deptno='&deptno';
(2).带有子查询的删除:
delete from emp where deptno=(select deptno from emp where ename='&ename');
PL/SQL中的流程控制语句
条件选择语句
1.if语句
if xxxx then xxxxxxx;
elsif xxx then xxxxxxx;
end if;
循环语句
1.loop循环
loop
循环的句子;
exit when退出的条件,一般是%notfound;
end loop;
loop
v_pro=v_pro*i;
exit when i>v_num;
end loop;
2.for循环
for i in lower_bound..upper_bound loop
句子;
end loop;
for i in 1..v_num loop;
v_pro:=v_pro*i;
end loop;
PL/SQL---EXCEPTION异常处理
知识点总结:
declare
变量名(自己定义的) exception;
begin
if 某一种情况 then
raise 定义异常变量名;
exception
when 定义的异常变量名 then
dbms_output.put_line('');//异常处理模块就是,输出变量名为xxx的处理结果
1.编写带有异常处理的PL/SQL程序,从键盘随机输入某个员工的编号,修改该员工的工资【异常处理】
declare
v_sal number;
v_empno number:='&empno';
e_lowsal exception;
e_midsal exception;
e_highsal exception;
begin
select sal into v_sal from emp
where empno=v_empno;
if v_sal<1000 then
raise e_lowsal;
elsif 1000<v_sal and v_sal<2000 then
raise e_midsal;
elsif 5000<v_sal then
raise e_highsal;
end if;
exception
when e_lowsal then
v_sal:=v_sal*1.3;
dbms_output.put_line('雇员工资更改为'||v_sal);
when e_midsal then
v_sal:=v_sal*1.2;
dbms_output.put_line('雇员工资更改为'||v_sal);
when e_highsal then
dbms_output.put_line('雇员工资高,不涨');
when no_data_found then
dbms_output.put_line('没找到这个雇员');
end;
PL/SQL---CURSOR游标的使用(一般用来查询数据,也可以用游标来修改和删除数据)
(1)游标基本使用
declare
cursor cursor-name(自定义游标名 )is select 属性列1,属性列2 from 表名 where 条件 FOR UPDATE;//定义游标中放什么东西,当修改和要删除数据时,加FOR UPDATE
emp-rec cursor-name %rowtype;//定义数据类型,这个记录变量是为了接受游标提取的数据,所以该变量的结构用游标来定义也是可以的
begin
open cursor-name;
//取多行数据时,需要使用记录变量 ,将游标中存的数据放到游标中去。,如果取很多条还要用到loop循环。
loop
fetch cursor-name into 记录变量名;
exit when cursor-name%notfound;//用到了游标的属性
dbms_output.put_line('');
end loop;
close cursor-name;
end;
修改语句:
UPDATE table-name set column = new-value WHERE CURRENT OF cursor-name;
DELETE table-name WHERE CURRENT OF cursor-name;
(2)带参数的游标,顾名思义就是游标参数是函数,可以往其中放变量的,f(x)。f是游标名,x是参数。当使用不同的参数值打开游标,可以产生不同的结果集
decare
cursor emp_cursor(游标参数名 数据类型)select ename from emp where deptno =游标参数; //这里的emp_cursor游标名可以自己定义,参数的数据类型要与从表中提取的属性列的数据类型一样
emp_rec emp_cursor %rowtype;
begin open emp_cursor(&游标参数名);
loop fetch emp_cursor into emp_rec; exit when emp_cursor %notfound; dbms_output.put_line(""); end loop;
close emo_cursor;
end;
//循环部分还可以用FOR循环,for循环包括了open 游标;和close 游标;这两步骤
decare
cursor ..................;//同上
begin for record_name in cursor_name loop dbms_output.put_line(""); end loop;
end;
(3)游标属性
%ISOPEN:用于确定游标是否打开
%FOUND:提取到数据
%NOTFOUND:没提取到数据
%ROWCOUNT:从结果集中已经获取到的记录的总数,获取到一条数据就增加1
2.编写利用游标操纵数据库的PL/SQL程序【游标的使用】
fetch的时候需要一个记录变量,然后我需要再对原表的一些数据进行改动,我想要游标能输出的是我已经改过的数据
利用游标操纵数据库,查询员工表中某一部门员工的姓名、年龄和工资,并输出。若员工工资小于1800元,则将其工资调整为1800元;若员工工资大于5000元,则将其工资调整为5000元。部门编号从键盘随机输入
代码:
declare
cursor app is select ename,age,sal from emp where deptno='&deptno' for update;
emp_rec app%rowtype;
begin
open app;
loop
fetch app into emp_rec;
exit when app%notfound;
if 5000<emp_rec.sal then emp_rec.sal:=5000;
elsif emp_rec.sal<1800 then emp_rec.sal:=1800;
end if;
dbms_output.put_line(emp_rec.ename||','||emp_rec.age||','||emp_rec.sal);
end loop;
close app;
end;