Oracle PL/SQL语言函数、匿名语句及循环

一、自定义函数

格式:

create or replace function 函数名(参数名 参数类型...)

    return  返回值类型

    as 

                  xx vachar2(20)                 --申明变量

                  begin                                --开始

                  --编写逻辑位置

    return xxxx

    end  函数名;                                --结束

例1:将名为WARD员工的工资和所有的员工的工资进行比较。

 1  --自定义函数
 2  create or replace function fn_emp_sal(asal in number,bname in varchar2)
 3  return varchar2 --返回值
 4  as                
 5           --申明变量
 6           vreturn varchar2(20);
 7           vsal emp.sal%type;    --vsal的数据类型与sal的数据类型一样
 8           begin
 9           --写逻辑的位置
10           --将查询出的sal赋值给vsal
11           select sal into vsal from emp where ename=bname;
12           if asal>vsal then
13              vreturn:='工资比'||bname||''||(asal-vsal);
14           elsif asal<vsal then
15              vreturn:='工资比'||bname||''||(vsal-asal);
16           else
17              vreturn:='工资一样';
18           end if;
19  return vreturn;
20  end  fn_emp_sal;              --结束 函数名,注意要加;

之后编译成功则显示:

进行查询:

1 select ename,sal,fn_emp_sal(sal,'WARD')from emp;

 执行结果:

二、匿名语句块 

      格式:

declare 

--声明变量

xx varchar2(20);

begin--开始

---编写逻辑位置

end;--结束

例1:工资小于3000的员工提薪3000。

 1  --匿名语句块
 2  declare 
 3        --声明变量
 4        vsal number;
 5        vename varchar2(20);
 6     begin 
 7        vename:='SMITH';     --静态写入
 8        --若想从控制台动态输入数据,则改成 vename:=&ename;
 9        --输入:'SMITH'
10        select sal into vsal from emp where ename=vename;
11        if vsal<3000 then 
12           update emp set sal=3000 where ename=vename;
13        end if;
14     end;

 执行结果:

 800->3000:

        

如将更新的数据添加文本中,可以再新建一个日志表和序列,增加一个触发器去记录更新的数据。

(1)增加一个日志表为t_logs:

(2)增加一个序列为log_seq:

(3)为表t_logs增加一个触发器log_xx,创建(序列中的主键):

(4)在上一段代码的第12行添加:

 1  insert into t_logs(txt) values(vename||'工资增加了'||(3000-vsal));

(5)执行结果:

 

例2:对收入低于1600 发奖金300  1600-2500发奖金200 2500-5000发奖金100。

 1 declare
 2     vename varchar2(20);
 3     vsal_comm emp.sal%type;
 4     vcomm emp.comm%type;
 5     vmsg varchar2(20);
 6     vrow emp%rowtype;---行变量 相当于Java的Object
 7   begin
 8     vename:='SMITH';
 9     select * into vrow from emp where ename=vename;
10     
11     --奖金
12     if vrow.comm is null then
13     vcomm:=0;
14     else
15     vcomm:=vrow.comm;
16     end if;
17     
18     --收入
19     vsal_comm:=vrow.sal+vcomm;
20     
21     if vsal_comm<1600 then
22       vmsg:='发奖金300';
23     elsif vsal_comm>1600 and vsal_comm<2500 then
24       vmsg:='发奖金200';
25     elsif vsal_comm>2500 and vsal_comm<5000 then
26       vmsg:='发奖金100';
27     else
28       vmsg:='不发';
29     end if;
30        DBMS_OUTPUT.PUT_LINE(vename||'过节费'||vmsg); --控制台输出
31 end;

点击查看->DBMS输出->点击+,连接该数据库,则会显示:

 

三、循环

1、loop 循环
例:循环输出1-10,相当于do...while

1 declare 
2     i number(2):=1;
3   begin 
4     loop
5     exit when i>10; ---循环结束条件
6     DBMS_OUTPUT.PUT_LINE(i);  --控制台输出
7     i:=i+1;
8     end loop;
9   end;

2、 while loop 循环

例:循环输出1-7,相当于while。

1 declare 
2    i number(2):=1;
3    begin 
4    while i<7 loop
5    DBMS_OUTPUT.PUT_LINE(i);
6    i:=i+1;
7    end loop;
8  end;

应用:例如插入1000条数据。

---恢复内容结束---

一、自定义函数

格式:

create or replace function 函数名(参数名 参数类型...)

    return  返回值类型

    as 

                  xx vachar2(20)                 --申明变量

                  begin                                --开始

                  --编写逻辑位置

    return xxxx

    end  函数名;                                --结束

例1:将名为WARD员工的工资和所有的员工的工资进行比较。

 1  --自定义函数
 2  create or replace function fn_emp_sal(asal in number,bname in varchar2)
 3  return varchar2 --返回值
 4  as                
 5           --申明变量
 6           vreturn varchar2(20);
 7           vsal emp.sal%type;    --vsal的数据类型与sal的数据类型一样
 8           begin
 9           --写逻辑的位置
10           --将查询出的sal赋值给vsal
11           select sal into vsal from emp where ename=bname;
12           if asal>vsal then
13              vreturn:='工资比'||bname||''||(asal-vsal);
14           elsif asal<vsal then
15              vreturn:='工资比'||bname||''||(vsal-asal);
16           else
17              vreturn:='工资一样';
18           end if;
19  return vreturn;
20  end  fn_emp_sal;              --结束 函数名,注意要加;

之后编译成功则显示:

进行查询:

1 select ename,sal,fn_emp_sal(sal,'WARD')from emp;

 执行结果:

二、匿名语句块 

      格式:

declare 

--声明变量

xx varchar2(20);

begin--开始

---编写逻辑位置

end;--结束

例1:工资小于3000的员工提薪3000。

 1  --匿名语句块
 2  declare 
 3        --声明变量
 4        vsal number;
 5        vename varchar2(20);
 6     begin 
 7        vename:='SMITH';     --静态写入
 8        --若想从控制台动态输入数据,则改成 vename:=&ename;
 9        --输入:'SMITH'
10        select sal into vsal from emp where ename=vename;
11        if vsal<3000 then 
12           update emp set sal=3000 where ename=vename;
13        end if;
14     end;

 执行结果:

 800->3000:

        

如将更新的数据添加文本中,可以再新建一个日志表和序列,增加一个触发器去记录更新的数据。

(1)增加一个日志表为t_logs:

(2)增加一个序列为log_seq:

(3)为表t_logs增加一个触发器log_xx,创建(序列中的主键):

(4)在上一段代码的第12行添加:

 1  insert into t_logs(txt) values(vename||'工资增加了'||(3000-vsal));

(5)执行结果:

 

例2:对收入低于1600 发奖金300  1600-2500发奖金200 2500-5000发奖金100。

 1 declare
 2     vename varchar2(20);
 3     vsal_comm emp.sal%type;
 4     vcomm emp.comm%type;
 5     vmsg varchar2(20);
 6     vrow emp%rowtype;---行变量 相当于Java的Object
 7   begin
 8     vename:='SMITH';
 9     select * into vrow from emp where ename=vename;
10     
11     --奖金
12     if vrow.comm is null then
13     vcomm:=0;
14     else
15     vcomm:=vrow.comm;
16     end if;
17     
18     --收入
19     vsal_comm:=vrow.sal+vcomm;
20     
21     if vsal_comm<1600 then
22       vmsg:='发奖金300';
23     elsif vsal_comm>1600 and vsal_comm<2500 then
24       vmsg:='发奖金200';
25     elsif vsal_comm>2500 and vsal_comm<5000 then
26       vmsg:='发奖金100';
27     else
28       vmsg:='不发';
29     end if;
30        DBMS_OUTPUT.PUT_LINE(vename||'过节费'||vmsg); --控制台输出
31 end;

点击查看->DBMS输出->点击+,连接该数据库,则会显示:

 

三、循环

1、loop 循环
例:循环输出1-10,相当于do...while

1 declare 
2     i number(2):=1;
3   begin 
4     loop
5     exit when i>10; ---循环结束条件
6     DBMS_OUTPUT.PUT_LINE(i);  --控制台输出
7     i:=i+1;
8     end loop;
9   end;

2、 while loop 循环

例:循环输出1-7,相当于while。

1 declare 
2    i number(2):=1;
3    begin 
4    while i<7 loop
5    DBMS_OUTPUT.PUT_LINE(i);
6    i:=i+1;
7    end loop;
8  end;

应用:例如插入1000条数据。

1 declare
2    i number(4):=1;
3   begin 
4    while i<1000 loop
5    insert into emp(enmae) values('老王'||i);
6    i:=i+1;
7    end loop;
8   end;

3、for in 循环

已知循环次数的循环控制语句。

1 declare 
2    begin
3       for i in 40..50 loop
4       DMBS_OUTPUT.PUT_LINE(i);
5       --for in 语句自动给i+1,故这里是个空操作,加不加null都可。
6       end loop;
7    end

  

原文地址:https://www.cnblogs.com/echola/p/9541041.html