PLsql学习笔记(三)
来源:互联网 发布:centos 部署java web 编辑:程序博客网 时间:2024/05/16 09:43
输入员工号码,显示雇员的姓名、工资、个人所得税(税率为0.03)
declare
c_tax_rate number(3,2):=0.03;
v_ename emp.ename%type;--数据类型联动,原先是什么类型,它就是什么类型。
v_sal emp.sal%type;
v_tax_sal number(7,2);
begin
select ename,sal into v_ename,v_sal from emp where empno=&no;
--计算所得税
v_tax_sal:=v_sal*c_tax_rate;
dbms_output.put_line('姓名'||v_ename||'工资'||v_sal||'交税'||v_tax_sal);
end;
--使用%type属性定义变量,它会按照数据库列来确定你定义的变量的类型和长度。标识符 表名.列名%type;
-----------------
复合变量(composite)
用于存放多个值的变量,主要包括:
pl/sql记录--类似于高级语言中的结构体
实例:
declare
--定义一个pl/sql记录类型emp_record_type
type emp_record_type is record(name emp.ename%type,salary emp.sal%type,title emp.job%type);
--定义了一个变量yzh_record,类型为emp_record_type
yzh_record emp_record_type;
begin
select ename,sal,job into yzh_record from emp where empno=12;
dbms_output.put_line('员工名:'||yzh_record.name);
end;
pl/sql表--相当于高级语言中的数组,但它的下标可以为负数
实例:
declare
--定义一个表类型yzh_table_type,用于存放emp.ename%type
--index by binary_integer表示下标是整数
type yzh_table_type is table of emp.ename%type index by binary_integer;
yzh_table yzh_table_type;
begin
select ename into yzh_table(0) from emp where empno=77;
dbms_output.put_line('员工名:'||yzh_table(0));
end;
参照变量:是指用于存放数组指针的变量。分为游标变量和对象类型变量
游标变量,定义游标时不需要制定select语句,但当使用游标时需要指定select 语句。
实例:编写一个块,可以输入部门号,并显示该部门所有员工的姓名和工资
declare
--定义游标类型
type yzh_emp_cursor is ref cursor;
--定义游标变量
test_cursor yzh_emp_cursor;
--定义变量
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
--执行,test_cursor与select 结合
open test_cursor for select ename,sal from emp where deptno=&no;
--循环取出值
loop
fetch test_cursor into v_ename ,v_sal;
--判断是否为空
exit when test_cursor%notfound;
dbms_output.put_line("用户名:"||v_ename||'工资:'||v_sal);
end loop;
end;
在上例基础上,如果工资低于200,则加100;
declare
--定义游标类型
type yzh_emp_cursor is ref cursor;
--定义游标变量
test_cursor yzh_emp_cursor;
--定义变量
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
--执行,test_cursor与select 结合
open test_cursor for select ename,sal from emp where deptno=&no;
--循环取出值
loop
fetch test_cursor into v_ename ,v_sal;
--判断工资高低
if v_sal<=200 then
update emp set v_sal=v_sal+100 where ename=v_ename;
end if;
--判断是否为空
exit when test_cursor%notfound;
dbms_output.put_line("用户名:"||v_ename||'工资:'||v_sal);
end loop;
end;
----------------------------------------------------
实例:编写一个过程,可以输入一个雇员号,如果该雇员的工资低于2000,就给该雇员工资增加10%
create or replace procedure mytest4(name varchar2) is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where ename=name;
if v_sal<2000 then
update emp set sal=sal*1.1 where ename=name;
end if;
end;
/
注意:不等于用"<>"表示,例如:comm<>0
多重循环语句结构:
if...then...elsif...then...else... endif;
----
循环结构:
loop...end loop;
实例:
编写一个过程,可输入用户名,并循环添加10个用户到users表中,用户编号从1开始
create table users(userNo number,userName varchar2(20));
create or replace procedure yzh_user(name varchar2) is
--定义变量,并初始化为1
v_num number:=1;
begin
loop
insert into users values(v_num,name);
--判断是否要退出循环
exit when v_num=10;
v_num:=v_num+1;
end loop;
end;
/
exec yzh_user('heizi');
----
循环从11开始
循环结构2
while 条件 loop ...end loop;
create or replace procedure yzh_user(name varchar2) is
--定义变量,并初始化为1
v_num number:=11;
begin
while v_num<20 loop
insert into users values(v_num,name);
v_num:=v_num+1;
end loop;
end;
/
循环结构3
基本结构
begin
for i in reverse 1..10 loop
insert into users values(i,'黑子');
end loop;
end;
/
循环结构4:goto语句 略
null空操作
- PLsql学习笔记(三)
- Oracle PLSQL 学习笔记三
- pLsql 学习笔记(-)
- PLsql学习笔记(四)
- PLSQL学习笔记(一)
- PLSQL学习笔记(二)
- oracle plsql 学习三
- pl/sql学习笔记之三 plsql basics
- plsql学习笔记(变量篇)
- plsql学习笔记(函数篇)
- plsql学习笔记(cursor篇)
- plsql 学习笔记1
- PLSQL学习笔记
- 学习plsql笔记
- 20120109plsql学习笔记
- plsql 学习笔记
- Oracle plsql学习笔记
- PLSQL学习笔记
- 自学号枯燥,学成不容易!
- 一回生二回熟
- 11月9日晚7点在线课堂:Flex 4与服务器端通信方式深度剖析
- T-SQL 基本语句考题
- 给text 添加文本,用AppendText()
- PLsql学习笔记(三)
- VC++的工程文件
- 熊猫的心愿
- 如何qq隐身可见?
- 心情
- jsp连接数据库的问题,谁来帮帮我啊····
- 我要加分
- 如何使用函数指针
- Dear,I Love you!