Oracle存储过程

plsql

 

官方文档

https://docs.oracle.com/database/121/LNPLS/toc.htm

 

PLSQl编写的基本语法

声明部分
处理部分
异常部分

最简单的案例

begin
   null;
end;

 

hello world

输出一下

begin
  dbms_output.put_line('hello world!');
end;

 

开启服务输入开关

不开启,看不到输出的信息

set serveroutput on;

 

 

包含声明部分-变量的使用

需要使用declare关键字

然后就是变量列表;

变量名 数据类型 ;

变量名 数据类型 := 值 ;

这里赋值不能使用= ,必须使用 :=

 

declare
  age Integer(3) := 99;

begin
  dbms_output.put_line(age);
end;



 

declare
  age Integer(3) := 99;

begin
  dbms_output.put_line(age);
  age := 66;
    dbms_output.put_line(age);
 
end;

 

输出连接

declare
  age Integer(3) := 99;
  v_name varchar2(999) := '刘德华';
begin
  dbms_output.put_line(v_name || '-' ||   age);
end;

 

在PLSQL当中使用函数

declare
  age Integer(3) := 99;
  v_name varchar2(999) := '刘德华有点小帅';
begin
  dbms_output.put_line(v_name || '-' ||   age);
  v_name := substr(v_name,2,3);
    dbms_output.put_line(v_name || '-' ||   age);
end;

常量的使用

age constant Integer(3) := 99;

 

存储过程

存储过程的创建

create or replace procedure pc_helloworld
as
begin
dbms_output.put_line('你存了吗?');
end;

使用存储过程

begin
  pc_helloworld;
end;

 

删除存储过程

drop procedure pc_helloworld;

 

 

用户交互

DECLARE
  age   INTEGER(3) := &age;
BEGIN
  dbms_output.put_line(age);
END;
   

 

 

DECLARE
  v_name   varchar2(223) := '&name';
BEGIN
  dbms_output.put_line(v_name);
END;
   

建议都使用'将后面的赋值部分包裹起来。

 

流程控制

if

DECLARE
  age   INTEGER(3) := '&age';
BEGIN
  IF age > 23 THEN
      dbms_output.put_line('小伙子,老了');
  END IF;
END;
  • if

  • end if 是成对的,不要省略了后面的end if;

 

if else

DECLARE
  age   INTEGER(3) := '&age';
BEGIN
  IF age > 23 THEN
      dbms_output.put_line('小伙子,老了');
  else
      dbms_output.put_line('小伙子,很年轻,棒棒');
  END IF;
END;
   

 

 

if elsif

DECLARE
  age   INTEGER(3) := '&age';
BEGIN
  IF age > 99 THEN
      dbms_output.put_line('老了');
  ELSIF age > 60 THEN
      dbms_output.put_line('还有要老了');
  ELSE
      dbms_output.put_line('还很年轻');
  END IF;
END;

它是elsif 不是 else if

 

case

和switch如出一辙

DECLARE
  age   INTEGER(3) := '&age';
BEGIN
  CASE age
      WHEN 88 THEN
          dbms_output.put_line('吉祥');
      WHEN 66 THEN
          dbms_output.put_line('如意');
      ELSE
          dbms_output.put_line('都不满意');
  END CASE;
END;

 

多重if

DECLARE
  age   INTEGER(3) := '&age';
BEGIN
  if age > 100 then
    dbms_output.put_line('老啊');
    if age mod 2 = 0 then
        dbms_output.put_line('偶数年份');
    end if;
  end if;
END;
   

 

基础循环

1-10打印

DECLARE
  vn   INTEGER(4) := 1;
BEGIN
  LOOP
      IF vn > 10 THEN
          EXIT;
      END IF;
      dbms_output.put_line(vn);
      vn := vn + 1;
  END LOOP;
END;

 

  • loop

  • end loop

  • 退出条件一定要记得添加,exit

 

exit when代替if退出

DECLARE
  vn   INTEGER(4) := 1;
BEGIN
  LOOP
      exit when vn > 10;
      dbms_output.put_line(vn);
      vn := vn + 1;
  END LOOP;
END;

 

 

continue使用

DECLARE
  vn   INTEGER(38) := 1;
BEGIN
  LOOP
      EXIT WHEN vn > 10;
      IF vn MOD 3 = 0 THEN
          vn := vn + 1;
          CONTINUE;
      END IF;

      dbms_output.put_line(vn);
      vn := vn + 1;
  END LOOP;
END;

 

for loop

DECLARE
  vn   INTEGER(38) := 1;
BEGIN
  FOR i IN 1..10 LOOP
      dbms_output.put_line(i);
  END LOOP;
END;

 

逆转循环

DECLARE
  vn   INTEGER(38) := 1;
BEGIN
  FOR i IN reverse 1..10 LOOP
      dbms_output.put_line(i);
  END LOOP;
END;
   

 

boolean数据类型

DECLARE
  v_is_love   BOOLEAN;
BEGIN
  v_is_love := true;
  IF v_is_love THEN
      dbms_output.put_line('真爱无悔');
  END IF;
END;

 

 

函数

创建函数

CREATE OR REPLACE FUNCTION mu_fuc (
  love IN VARCHAR2
) RETURN VARCHAR2 IS
  my_love   VARCHAR2(255);
BEGIN
  my_love := concat('有点帅',love);
  return(my_love);
END;

 

使用函数

declare
  v_love varchar2(255);
begin
  v_love := mu_fuc('刘德华');
    dbms_output.put_line(v_love);
end;

 

 

删除函数

drop function mu_fuc;

 

函数案例

根据传入的的ID,查找到值赋值给fullname,并返回。

可以使用select into语法,完成赋值。

create or replace function f_get_fullname(id in number)
  return varchar2
   is fullname varchar2(255);
begin
   select concat(concat(first_name,' '),last_name) into fullname
   from contacts
   where contact_id = id;
  return(fullname);
end;


begin
    dbms_output.put_line(f_get_fullname(18));
end;

 

函数的参数模型

  • in

  • out

  • in out

 

in 代表只能入值,但是在过程中不能修改。

下面的ID为in模式,而又进行了修改,则是错误的。

create or replace function f_get_fullname(id in number)
  return varchar2
   is fullname varchar2(255);
begin
   select concat(concat(first_name,' '),last_name) into fullname
   from contacts
   where contact_id = id;
  return(fullname);
end;


begin
    dbms_output.put_line(f_get_fullname(18));
end;

 

 

out模式

就是对外部修改是有效的


CREATE OR REPLACE FUNCTION hello_world (
  msg OUT VARCHAR2
) RETURN VARCHAR2 IS
  a   VARCHAR2(255);
BEGIN
  msg := '雪花飘飘,北风萧萧';
  return(msg);
END;

 

declare
  a varchar2(255);
  b varchar2(255) := '哎呦';
begin
  b := hello_world(a);
  dbms_output.put_line(a);
  dbms_output.put_line(b);
end;

 

in out

 

函数和过程的最大区别

就是过程不像函数有返回值,return语句,PLSQL当中解释的时候,没有赋值语句理解为过程,有则理解为函数。

CREATE OR REPLACE FUNCTION hello_world (
  msg in OUT VARCHAR2
) RETURN VARCHAR2 IS
  a   VARCHAR2(255);
BEGIN
  msg := '雪花飘飘,北风萧萧';
  return(msg);
END;


declare
  a varchar2(255);
  b varchar2(255) := '哎呦';
begin
  b := hello_world(a);
  dbms_output.put_line(a);
  dbms_output.put_line(b);
end;

 

 

函数综合案例

可以使用我们自定义的函数用于select语句当中

CREATE OR REPLACE FUNCTION f_get_fullname (
  id IN NUMBER
) RETURN VARCHAR2 IS
  fullname   VARCHAR2(255);
BEGIN
   SELECT
      concat(concat(first_name,' '),last_name)
   INTO fullname
   FROM
      contacts
   WHERE
      contact_id = id;

  return(fullname);
END;

SELECT
  f_get_fullname(18)
FROM
  dual;

 

原文地址:https://www.cnblogs.com/mobies/p/11666832.html