一、IF
1.
if 条件 then....;
end if;
2.
if 条件 then.....;
else.....
end if;
3.
if 条件 then.....;
elsif 条件 then....
else ....
end if;
三、while do..while
do {} while (i<5)
1.
while 条件
loop
...
end loop;
2.
loop
....
exit when i>5;
end loop;
四、for
for i in 1.....10
loop
..
end loop;
五、存储过程
语法基本结构
create [or replace]producedure 过程名(参数列表)
as
begin
......
end;
无参:
有参:
--传入一个员工编号,给该员工帐500 create or replace PROCEDURE raiseSalary(pid in number) as psal emp.sal%type; begin select sal into psal from emp where empno=pid; update emp set sal = sal +500 where empno=pid; DBMS_OUTPUT.put_line(psal || '-------' || psal+500); end;
六、存储函数
与存储过程最大的区别:必须有return
语法
create [or replace] function 函数名(参数列表)
rerurn 数据类型
as
....
begin
.....
end;
create or replace function getTotalSal(pid in number) return number as empSal emp.sal%type; empComm emp.comm%type; begin select sal ,comm into empSal,empComm from emp where empno=pid;
return empSal*12+ nvl(empComm,0);
end;
总结:存储过程/存储函数?
只有一个返回值适合使用,存储函数return
没有、多个返回值适合使用,存储过程 out out。。。
存储过程:传入员工编号,传入姓名、工作
存储函数:传入员工编号,传入姓名、工作
六、包、包头和包体的使用
使用
CREATE OR REPLACE PACKAGE BODY PACKAGE AS
procedure queryEmpList(dno in number,empList out empcursor) AS BEGIN open empList for SELECT * FROM emp where deptno=dno; END queryEmpList;
END PACKAGE;
验证
desc package