一、先简单说一下DDL、DML、DCL的区别
DDL(数据定义语句) 例如 drop create alter truncate
DML(数据操控语句) 例如 select update insert merge
DCL(数据控制语句) 例如 grant recoke
事物控制语句 commit rollback
二、游标(cursor)
游标的基本语法:
declare
--首先游标需要声明
cursor c is select from emp;
impino emp%rowtype;
begin
--首先记住 在使用游标的时候 一定要先打开游标
open c;
--让游标往下移动
fetch c into impino;
--判断是否有数据发现
while(c%found)loop
dbms_output.put_line(impino.enam);
fetche c into impino;
end loop;
end;
/
三、函数
函数就是一个返回值的过程
eg:根据雇员的编号查出雇员的年薪
create or replace function myfun(eno emp.empno%type) return number
as
rsal number
begin
--直接通过select 语句进行查找
select( sal+nvl(comm,0))*12 into rsal from emp where empno =eno;
return rsal;
end;
/
----直接调用该函数来查询
select myfun(7369 ) from dual;
四、存储过程(procedure)
--传入部门的编号,部门的名称,部门的位置,之后调用此过程就可以完成对部门的操作
create or replace produce mypro(dno dept.deptno%type,name dept.dname%type,dl dept.loc%type)
as
cou number;
begin
--先通过部门编号来判断表中是否有这个部门的信息
select count(deptno) into cou from dept where deptno=dno;
if(cou=0)then
--可以增加部门信息
dbms_output.put_line('存入信息成功‘);
else
dbms_output.put_line('存入信息失败');
end if;
end;
/
----执行存储过程
declare
deptno dept.deptno%type;
begin
deptno:=40;
mypro(deptno,name,dl);
end;
/