oracle学习(使用了PL/SQL)——2

PL/SQL语言快
作用:如果不使用PL/SQL语言,oracle一次只能处理一条SQL语句。每条SQL语句都导致客户(client)向服务器(server)调用,从而在性能上产生很大的开销,尤其是在网络操作中。如果使用PL/SQL,一个块中的语句作为一个组,导致客户向服务器的一次调用,减少网络转输。

1.匿名块
描述:也就是没有命名的PL/SQL块,它可以是嵌入某一个应用之中的一个PL/SQL块。匿名块是出现在应用程序中的没有名字且不存储到数据库中的块。它们可以调用其他程序,却不能被其他程序调用。

declare
    ndname varchar(14); --声明变量,可以声明多个
begin
    ndname := &newName;   --赋值操作符 :=      &后跟任意名称,用来取用户输入值
    update dept set dname=ndname where deptno=3;
end;

这里写图片描述

2.%type,%rowtype,%record table用法(可用在匿名块、函数、过程中)
这里在匿名块中使用演示
①%type 单个变量类型

declare
    noid dept.deptno%type; --获取deptno的类型,即number(2),声明单个变量时做好这样做
begin
    noid := &no;
    update dept set dname='前进一小步' where deptno=noid;
end;

②%rowtype 表中一行变量的所有相对应的类型

declare
    row_emp emp%rowtype;
    t_empno emp.empno%type;
begin
    t_empno := &eno;
    --查询这一行数据到row_emp中
    select * into row_emp from emp where empno=t_empno;
    --这里的ename和hiredate还是emp表中的列名
    dbms_output.put_line('当前员工的姓名为:' || row_emp.ename || ',雇佣日期为:' || row_emp.hiredate);
    --可能会发生的异常,即表中编号没有输入的编号时发生
    exception
        when no_data_found then
           dbms_output.put_line('您输入的员工编号不正确');
end;

这里写图片描述
③record与table
①record

declare 
--样式有点类似c中的结构体
type emp_record_type is RECORD(
--定义记录类型和记录变量
  ename emp.ename%type,
  sal emp.sal%type,
  comm emp.comm%type,
  total_sal emp.sal%type
);
    v_emp_record emp_record_type;  --v_emp_record为emp_record_type的引用
begin
  select ename,sal,nvl(comm,0),sal+nvl(comm,0) into v_emp_record
  from emp where empno=7369;
  dbms_output.put_line('员工姓名:'|| v_emp_record.ename);
  dbms_output.put_line('基本工资:'|| v_emp_record.sal);
  dbms_output.put_line('奖金:'|| v_emp_record.comm);
  dbms_output.put_line('实发工资:'|| v_emp_record.total_sal);
end;

这里写图片描述
②table

--将部门编号是1020的部门信息存储到table类型中,然后输出其信息
declare
--定义类型为table的集合,取任意表一行的列的类型作为table的类型
  type dept_table_type is table of dept%rowtype index by binary_integer;--binary_integer是number的子集,专门用于数组的下标
  table_type dept_table_type;
begin
  select * into table_type(0) from dept where deptno=10;
  select * into table_type(1) from dept where deptno=20;
  dbms_output.put_line('编号为10的部门:'|| table_type(0).dname || ',地址:' || table_type(0).loc);
  dbms_output.put_line('编号为20的部门:'|| table_type(1).dname || ',地址:' || table_type(1).loc);
end;

这里写图片描述
3.条件控制
① if 。。。then elsif。。。else。。。end if;

/*输入员工编号,如果该员工
原来没有奖金,则按照工资的10%发放 
原来有奖金但不超过1000的,补到1000;
其余的按照原来奖金基础再加上10%发放;*/

declare
  t_empno emp.empno%type;
  t_comm emp.comm%type;
begin
  t_empno := &empno;
  select comm into t_comm from emp where empno=t_empno;
  if t_comm = '' or t_comm is null then
    update emp set comm = sal*0.1 where empno = t_empno;
  elsif t_comm<1000 then
    update emp set comm = 1000 where empno = t_empno;
  else
    update emp set comm = comm+sal*0.1 where empno = t_empno;
  end if;
  exception
     when no_data_found then
        dbms_output.put_line('您输入的员工编号不正确');
end;

②case when。。。then 。。。。end case;

--根据员工编号输出员工工资级别(1000以下C,1-2000 B,2000以上A)
declare
    t_emp_sal emp.sal%type;
    t_empno emp.empno%type;
begin
    t_empno := &eno;
    select sal into t_emp_sal from emp where empno=t_empno;
    case when t_emp_sal<1000 then
      dbms_output.put_line('工资级别为C');
    when t_emp_sal<2000 then 
      dbms_output.put_line('工资级别为B'); 
    when t_emp_sal>2000 then 
      dbms_output.put_line('工资级别为A');
    end case;
    exception
     when no_data_found then
        dbms_output.put_line('您输入的员工编号不正确');   
end;

循环控制
①loop 。。。 end loop;

declare 
    type dept_table_type is table of dept%rowtype
    index by binary_integer;
    i number(1):=0;
    v_dept_table dept_table_type;
begin
    v_dept_table(0).deptno:='50';
    v_dept_table(0).dname:='研发部';
    v_dept_table(0).loc:='北京';
    v_dept_table(1).deptno:='60';
    v_dept_table(1).dname:='开发部';
    v_dept_table(1).loc:='上海';
    v_dept_table(2).deptno:='70';
    v_dept_table(2).dname:='推广部';
    v_dept_table(2).loc:='北京';
    loop
         if i>2 then exit; end if;
            insert into dept values
                     (v_dept_table(i).deptno,v_dept_table(i).dname,v_dept_table(i).loc);
            i:=i+1;
    end loop;
end; 

②for var_i in 0..10(或者)
语法:
FOR 循环变量 in [REVERSE] 初值表达式..终值表达式 LOOP
语句段;
END LOOP;

declare 
    type dept_table_type is table of dept%rowtype
    index by binary_integer;
    i number(1):=0;
    v_dept_table dept_table_type;
begin
    v_dept_table(0).deptno:='50';
    v_dept_table(0).dname:='研发部';
    v_dept_table(0).loc:='北京';
    v_dept_table(1).deptno:='60';
    v_dept_table(1).dname:='开发部';
    v_dept_table(1).loc:='上海';
    for i in 0..v_dept_table.count-1 loop  --在这里i可以不用再上面定义,
    --for i in v_dept_table.first..v_dept_table.last loop
        insert into dept values
            ( v_dept_table(i).deptno,v_dept_table(i).dname,v_dept_table(i).loc);
        end loop;
end;

③ while 。。。 loop 。。。end loop;

declare 
    type dept_table_type is table of dept%rowtype
    index by binary_integer;
      i number(1):=0;
    v_dept_table dept_table_type;
begin
    v_dept_table(0).deptno:='50';
    v_dept_table(0).dname:='研发部';
    v_dept_table(0).loc:='北京';
    v_dept_table(1).deptno:='60';
    v_dept_table(1).dname:='开发部';
    v_dept_table(1).loc:='上海';
    while i<2 loop
        insert into dept values( v_dept_table(i).deptno,v_dept_table(i).dname,v_dept_table(i).loc);
      i:=i+1;
    end loop;
end;

转载请注明出处:http://blog.csdn.net/sinat_30035833/article/details/78394527

猜你喜欢

转载自blog.csdn.net/sinat_30035833/article/details/78394527