--创建分区表
create table sales
(
id number(4) ,
selldate date,
descript varchar2(20)
)
partition by range(selldate)
(
partition p1 values less than (to_date('2017-4-1','yyyy-MM-dd')),
partition p2 values less than (to_date('2017-7-1','yyyy-MM-dd')),
partition p3 values less than (to_date('2017-11-1','yyyy-MM-dd')),
partition p4 values less than (maxvalue)
);
insert into sales values(1,to_date('2017-4-1','yyyy-MM-dd'),'一季度')
select * from sales partition (p2)
declare --声明
-- 变量常量命名规范 : v 变量 c 常量
v_name varchar2(20) ;
c_const varchar2(20) :='abc';
v_gender number(1) := 3;
v_cnnt number(20) := 1;
v_ename scott.emp.ename%type; -- 跟随数据库字段类型
v_rec scott.emp%rowtype;--行类型
e_comm_isnull exception;--自定义异常
begin --执行部分
--raise_application_err('-200001','abc') oracle 自带的报告错误存储过程
--if end
dbms_output.put_line('--if end-- ');
v_name := 'oracle';
if v_name is null then
dbms_output.put_line('v_name is null');
elsif (v_name = 'ddd') then -- 这里是 elsif 并不是 else if
dbms_output.put_line('ddd');
else
dbms_output.put_line(v_name);
end if;
--case end
dbms_output.put_line('--case end-- ');
case v_gender
when 1 then
dbms_output.put_line('男');
when 2 then
dbms_output.put_line('女');
else
dbms_output.put_line('无性别者');
end case;
--loop end
dbms_output.put_line('--loop end-- ');
loop
--循环体
v_cnnt := v_cnnt + 1;
dbms_output.put_line(v_cnnt);
exit when v_cnnt >10; --exit when <退出条件>
end loop;
--while end
dbms_output.put_line('--while end--');
while v_cnnt >0 loop
v_cnnt := v_cnnt - 1;
dbms_output.put_line(v_cnnt);
end loop;
--for end
dbms_output.put_line('--for end--');
for i in 1..6 loop
dbms_output.put_line(i);
end loop;
--变量跟随表字段类型
select ename into v_ename from scott.emp where empno = 7369;
dbms_output.put_line(v_ename);
--行类型
select * into v_rec from scott.emp where empno = 7369;
dbms_output.put_line(v_rec.empno || ' '|| v_rec.ename); --变量名.数据库列名
--自定义异常
/**
select * into v_rec from scott.emp where empno = 7566;
if v_rec.comm is null then
raise e_comm_isnull; -- 手动抛出自定义异常
end if;
*/
exception
/**
select * into v_rec from scott.emp;
dbms_output.put_line(v_rec.empno || ' '|| v_rec.ename);
*/
when too_many_rows then --异常类型
dbms_output.put_line('返回数据大于一'); --处理异常
when e_comm_isnull then --异常类型
dbms_output.put_line('用户奖金为空'); --处理异常
end;
--游标
/**
游标的基本定义
在执行有一个select,insert,update和delete 语句的pl/sql块时,
oracle 会在内存中为其分配一个缓冲区,将执行结果放在这个缓冲区中,而游标是指向该区域的一个指针。
游标为应用程序提供了一中对多行数据查询结果集中的每行数据进行单独处理的方法,
是设计嵌入式sql语句的应用程序的常用编程方式。
游标的分类
oracle 提供了两种游标,静态游标和动态游标,静态游标是在编译时知道明确的select语句的游标,
静态游标又分为显式游标和隐式游标,无论是显式游标还是隐式游标,都具有%found、%notfound、%isopen、%rowcount 4个属性
%notfound fetch是否提到数据 没有true 提到false
%found fetch是否提到数据 有true 没提到false
%rowcount 返回sql语句影响的行数
%isopen 布尔值 游标是否打开
*/
--静态显示游标
declare
v_rec scott.emp%rowtype;
cursor csr_emp is select * from scott.emp; --定义一个游标
begin
open csr_emp;
--读取游标
loop
fetch csr_emp into v_rec;
dbms_output.put_line(v_rec.empno || ' ' || v_rec.ename );
exit when csr_emp%notfound;
end loop;
close csr_emp;
end;
-- 明天再更
pl/sql 基础
最新推荐文章于 2021-12-05 19:21:00 发布