Oracle PL/SQL 编程基础 实例 2
来源:互联网 发布:服装打版软件 编辑:程序博客网 时间:2024/05/17 17:14
if 循环 控制语句
if--then endif
if----then ----else endif
if-----then --elsif then ----else endif
--编写一个过程,可以 输入一个雇员名,如果该雇员的工资低于2000就给他增加10%
create or replace procedure sp_pro6(spName varchar2) is
v_sal emp.sal %type;
begin
select sal into v_sal from emp where ename =spName;
--判断
if v_sal<2000 then
update emp set sal=sal*1.1 where ename =spName;
end if;
end;
--======####案例s33 编写一个过程,可以 输入一个雇员名,如果该雇员的补助不是0就在原基础上增加100,如果是0就加200
create or replace procedure sp_pro7(spName varchar2) is
v_comm emp.comm %type;
begin
select comm into v_comm from emp where ename =spName;
--判断
if v_comm<>0 then
update emp set comm=comm+100 where ename =spName;
else
update emp set comm=comm+200 where ename =spName;
end if;
end;
----========循环 loop end loop
-----案例 编写一个过程 可输入用户名 并添加10个用户到users表中 用户编号从1来时增加
--建个表
create table users1(uid1 number,uname varchar2(40));
create or replace procedure sp_pro8(spName varchar2) is
--定义变量
v_num number :=1;
begin
loop
insert into users1 values(v_num,spName);
--判断是否退出循环
exit when v_num =10;
--自增
v_num:=v_num+1;
end loop;
end;
----------------===while ...loop end loop
----===案例 从11 开始 添加10个用户
create or replace procedure sp_pro8(spName varchar2) is
--定义变量
v_num number :=11;
begin
while v_num<=20
loop
--执行
insert into users1 values(v_num,spName);
--自增
v_num:=v_num+1;
end loop;
end;
---------------------for
begin for i in reverse 1.. 10 loop
insert into users1 values(v_num,spName);
end loop;
end;
-----------------顺序控制语句 goto null
goto label
<<label>>
-----=-----------返回结果集的过程----=======
---1.----创建 一个 包 在该包中 定义一个 类型 test_cursor 是个游标
Create or replace package testpackage as
type test_cursor is ref cursor;
end testpackage;
-----2.创建过程
create or replace procedure sp_pro9 (spNO in number,p_cursor out testpackage.test_cursor)
is
begin
open p_cursor for select * from emp where deptno=spNO;
end;
-----3.如何在java中调用
---1.创建 Callablestatement cs =ct.prepareCall([call sp_pro9(?,?)]);
---- cs.setInt(1,10);
----cs.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);
--执行--cs.execute();
--得到结果集
/*ResultSet rs=(ResultSet)cs.getObject(2);
while(rs.next()){
....
}*/
---------------------例外处理---------
case_not_found
data_not_found
cursor_already_open
dup_val_on_index 唯一索引重复
invaild_cursor 在不合法的游标上执行操作 比如 从没有打开的游标取值 或关闭没有打开的游标
invalid_number
too_many_rows select into 的时候 返回超过一行
zero_divide 2/0
value_error 变量长度不足以容纳实际长度
-----自定义例外
create or replace procedure ex_test(spNO number)
is
--定义一个例外
myex exception;
begin
update emp set sal=sal+1000 where empno=spNO;
--sql%notfound 表示没有update
--raise myex 触发myex
if sql%notfound then
raise myex;
end if;
exception
when myex then
dbms_output.put_line('没有更新任何用户');
end;
-----------------------------视图---------------
--视图不能添加索引
create view myview as select * from emp where sal<1000;
select * from myview;
if--then endif
if----then ----else endif
if-----then --elsif then ----else endif
--编写一个过程,可以 输入一个雇员名,如果该雇员的工资低于2000就给他增加10%
create or replace procedure sp_pro6(spName varchar2) is
v_sal emp.sal %type;
begin
select sal into v_sal from emp where ename =spName;
--判断
if v_sal<2000 then
update emp set sal=sal*1.1 where ename =spName;
end if;
end;
--======####案例s33 编写一个过程,可以 输入一个雇员名,如果该雇员的补助不是0就在原基础上增加100,如果是0就加200
create or replace procedure sp_pro7(spName varchar2) is
v_comm emp.comm %type;
begin
select comm into v_comm from emp where ename =spName;
--判断
if v_comm<>0 then
update emp set comm=comm+100 where ename =spName;
else
update emp set comm=comm+200 where ename =spName;
end if;
end;
----========循环 loop end loop
-----案例 编写一个过程 可输入用户名 并添加10个用户到users表中 用户编号从1来时增加
--建个表
create table users1(uid1 number,uname varchar2(40));
create or replace procedure sp_pro8(spName varchar2) is
--定义变量
v_num number :=1;
begin
loop
insert into users1 values(v_num,spName);
--判断是否退出循环
exit when v_num =10;
--自增
v_num:=v_num+1;
end loop;
end;
----------------===while ...loop end loop
----===案例 从11 开始 添加10个用户
create or replace procedure sp_pro8(spName varchar2) is
--定义变量
v_num number :=11;
begin
while v_num<=20
loop
--执行
insert into users1 values(v_num,spName);
--自增
v_num:=v_num+1;
end loop;
end;
---------------------for
begin for i in reverse 1.. 10 loop
insert into users1 values(v_num,spName);
end loop;
end;
-----------------顺序控制语句 goto null
goto label
<<label>>
-----=-----------返回结果集的过程----=======
---1.----创建 一个 包 在该包中 定义一个 类型 test_cursor 是个游标
Create or replace package testpackage as
type test_cursor is ref cursor;
end testpackage;
-----2.创建过程
create or replace procedure sp_pro9 (spNO in number,p_cursor out testpackage.test_cursor)
is
begin
open p_cursor for select * from emp where deptno=spNO;
end;
-----3.如何在java中调用
---1.创建 Callablestatement cs =ct.prepareCall([call sp_pro9(?,?)]);
---- cs.setInt(1,10);
----cs.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);
--执行--cs.execute();
--得到结果集
/*ResultSet rs=(ResultSet)cs.getObject(2);
while(rs.next()){
....
}*/
---------------------例外处理---------
case_not_found
data_not_found
cursor_already_open
dup_val_on_index 唯一索引重复
invaild_cursor 在不合法的游标上执行操作 比如 从没有打开的游标取值 或关闭没有打开的游标
invalid_number
too_many_rows select into 的时候 返回超过一行
zero_divide 2/0
value_error 变量长度不足以容纳实际长度
-----自定义例外
create or replace procedure ex_test(spNO number)
is
--定义一个例外
myex exception;
begin
update emp set sal=sal+1000 where empno=spNO;
--sql%notfound 表示没有update
--raise myex 触发myex
if sql%notfound then
raise myex;
end if;
exception
when myex then
dbms_output.put_line('没有更新任何用户');
end;
-----------------------------视图---------------
--视图不能添加索引
create view myview as select * from emp where sal<1000;
select * from myview;
0 0
- Oracle PL/SQL 编程基础 实例 2
- Oracle PL/SQL 编程基础 实例
- Oracle PL/SQL 编程基础
- oracle pl/sql 编程基础
- Oracle数据库编程:PL/SQL编程基础
- oracle之PL/SQL编程基础
- Oracle的pl/sql编程基础
- Oracle之PL/SQL编程基础
- Oracle之PL/SQL编程基础
- Oracle PL/SQL DBA 编程实践基础
- Oracle PL/SQL编程(2)
- oracle pl/sql实例
- ORACLE PL/SQL 基础
- ORACLE PL/SQL 基础
- oracle pl/sql 基础
- oracle pl/sql 基础
- Oracle PL/SQL 基础
- Oracle PL/SQL 基础
- trees in a row
- jquery组件团购倒计时功能
- 最后一次了
- 启动loadrunner 11的controller提示试图执行系统不支持的操作
- 浅析 Linux 中的时间编程和实现原理,第 1 部分: Linux 应用层的时间编程
- Oracle PL/SQL 编程基础 实例 2
- 江铠同疑证实李湘怀二胎:小恬妞要当姐姐啦
- [Java][activiti]同步或者重构activiti identify用户数据的方法
- 修改滚动条信息
- android初学-----解决ListView 中item 里面的按钮不能单击的问题
- 对Android开发者有益的40条优化建议
- 啊啊啊啊
- day60(5.10)
- C++ GUI QT 第4版 之线程 -------总结