设为首页 加入收藏

TOP

oracle初学知识点总结(二)
2014-11-24 02:30:51 来源: 作者: 【 】 浏览:4
Tags:oracle 初学 知识点 总结
me1;
end;
function f_returnpasswd(name1 varchar2) return varchar2 is
pwd varchar2(30);
begin
select passwd into pwd from mytable where name=name1;
return pwd;
end;
end;
--怎样调用包中的过程和函数?
--调用过程
exec p_package.f_returnpasswd('wucaiming');
--调用函数
var a varchar2(30);
call p_package.f_returnpasswd('wucaiming') into:a;
触发器:触发器是隐含的执行的存储过程。触发器
pl/sql中的变量类型有4类:
1.标量类型(scalar)
2.复合类型(composite)
3.参考类型(reference)
4.clob(large object)
1.标量
v_name varchar2(20); --变长字符串
v_sal number(6,2); ---9999.99~+9999.99
v_sql2 number(6,2):=5.4; --定义一个小数并赋初值为5.4,":="相当于java中的"="赋值运算符;
v_hiredate date; --定义一个日期型数据
v_valid boolean not null default false;
eg1:--把'wucaiming'的工资涨一倍,并输出
set serveroutput on;
declare
v_name varchar2(30);-- v_name mytable.name%type;
v_sal_add number(6,2);
begin
select name,sal*2 into v_name ,v_sal_add from mytable where name=&name;
dbms_output.put_line('姓名:'||v_name||' 工资:'||v_sal_add);
end;
2.复合变量
pl/sql记录
pl/sql表
嵌套表
varray
--pl/sql记录
类似于高级语言中的结构体(或者类)
set serveroutput on;
--定义一个plsql 记录 类型;
declare type mytable_record_type is record(
v_name mytable.name%type,
v_passwd mytable.passwd%type,
v_sal mytable.sal%type
);
--定义了一个变量,变量的类型是上面定义的记录类型;
r_record mytable_record_type;
begin
select name,passwd,sal into r_record from mytable where name='wucaiming';
dbms_output.put_line('姓名:'||r_record.v_name||' 密码:'||r_record.v_passwd||' 工资:'||r_record.v_sal);
end;
--pl/sql表
表相当于高级语言中的数组。这里的数组下标可以为负;
set serveroutput on;
declare type mytale_table_type is table of mytable.name%type index by binary_integer;
t_table mytale_table_type;
begin
select name ,passwd ,sal into t_table(-1),t_table(0),t_table(1) from mytable where name='wucaiming';
dbms_output.put_line('姓名:'||t_table(-1)||' 密码:'||t_table(0)||' 工资:'||t_table(1));
end;
3.参照变量 之游标变量
定义游标不需要select语句,使用游标需要使用select语句;
declare
type mytable_cursor is ref cursor;
test_cursor mytable_cursor;
v_name mytable.name%type;
v_passwd mytable.passwd%type;
v_sal mytable.sal%type;
begin
--游标与一个select关联;
open test_cursor for select name,passwd,sal from mytable where name='wucaiming';
loop
fetch test_cursor into v_name,v_passwd,v_sal;
--判断游标是否为空;
exit when test_cursor%notfound;
dbms_output.put_line('姓名:'||v_name||' 密码:'||v_passwd||' 工资:'||v_sal);
end loop;
end;
pl/sql的高级用法:(能编写高级过程,下订单过程模块)
pl/sql的控制结构:
if...then
if...then...else
if...then...elsif...then...else
eg:
create or replace procedure p_addsal(name1 varchar2) is
v_sal mytable.sal%type;
begin
select sal into v_sal from mytable where name=name1;
if v_sal <2000 then
update mytable set sal=200 where name=name1;
end if;
end;
二层条件分支:
eg:
create or replace procedure p_addsal(name1 varchar2) is
v_sal mytable.sal%type;
begin
select sal into v_sal from mytable where name=name1;
if v_sal <200 then
update mytable set sal=200 where name=name1;
else
update mytable set sal=v_sal+100 where name=name1;
end if;
end;
三层分支:
eg:
create or replace procedure p_addsal(name1 varchar2) is
v_sal mytable.sal%type;
begin
select sal into v_sal from mytable where name=name1;
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2014年第1季度OraclePSU更新(For1.. 下一篇oracle知识点小节2(字符串中包含..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: