oracle plsql的结构

declare 

变量声明部分(没有变量可以不写declare)

begin

执行部分

exception

异常处理部分

end

*:declare 和 exception部分是可选的

(1)PL/sql中变量的定义

格式1

变量名 变量类型 【约束】 default 默认值

格式2

变量名 变量类型 【约束】【:=初始值】

(2)一个简单的PL/sql块

declare

   i number :=99;

begin

   i:=i+10;

   dbms_output.put_line(i);

end;

/

PL/sql块中流程控制

一。if语句

if 条件 then

。。。

end if;

if 条件 then

。。。

else

。。。

end if;

declare
    zhangsan number default 24;
    lisi number :=28;
    begin
    if zhangsan<lisi then
    dbms_output.put_line('zhangsan younger than lisi');
    end if;
    end;
    /

if 条件 then

...

elsif 条件 then

...

end if;

declare
          age number default 90;
          height number :=175;
          gender char(2):='';
begin
          if gender='' then
              dbms_output.put_line('你可以和女性结婚');
          end if;
           if height>170 then
              dbms_output.put_line('可以打篮球');
          else 
               dbms_output.put_line('可以踢足球');
          end if;

          if age<20 then
               dbms_output.put_line('年轻小伙');
           elsif age <= 50 then
               dbms_output.put_line('年轻有为');
           elsif age <=70 then
                dbms_output.put_line('安享天伦');
           else  
                dbms_output.put_line('佩服佩服');
           end if;
end;

二。While循环语句:

while 条件 loop

循环体

end loop;

declare
age number default 1;
begin
while age <=10 loop
dbms_output.put_line('age is'||age);
age:=age+1;
end loop;
end;

三。For循环结构:

 for 循环变量 in [reverse] 起始值..终止值 loop

    循环体

 end loop;

begin
    for i in 1..9 loop
        dbms_output.put_line(i);
    end loop;
    for i in reverse 1..9 loop
        dbms_output.put_line(i);
    end loop;
end;

四。Loop循环语句:

loop

循环体

exit when 条件;

end loop;

//计算1-100的和
declare
          i number:=0;
          total number:=0;
begin
          loop
                i:=i+1;
                total:=total+i;
                if i=100 then
                        exit;
                end if;
          end loop;
          dbms_output.put_line('总和'||total);
end;

跳出loop的方法

declare
    i number :=0;
    total number :=0;
begin
    loop
        i := i+1;
        total := total + i;

        exit when i>=100;
    end loop;

    dbms_output.put_line('总和'||total);

end;

存储过程创建语法:

create or replace procedure 名称[(参数)]
authid current_user|definer --以定义者还是调用者的身份运行
is[不要加declare]
变量声明部分
begin
主体部分
exception
异常部分
end;
create procedure a1
is
    begin
    dbms_output.put_line('hello');
    end;
    /

调用

Call a1();

create procedure a2(width int,height int)
    is
    area int:=0;
    begin
    area:=width*height;
    dbms_output.put_line('area is'||area);
    end;
    /

调用

Call a2(20,3);

原文地址:https://www.cnblogs.com/111wdh/p/13634608.html