【Oracle】存储过程基本语法

た 入场券 2023-06-21 13:24 75阅读 0赞

1、基本语法

  1. CREATE OR REPLACE PROCEDURE 存储过程名(param1 in type,param2 out type)
  2.  /** AS和IS没区别 **/
  3.  IS
  4.  变量1 类型(值范围);
  5.  变量2 类型(值范围);  
  6.  BEGIN
  7.    select count(*) into 变量1 from 表名 where 列名=param1;
  8.    if (判断条件) then
  9.      select 列名 into 变量2 from 表名 where 列名=param1;
  10.      DBMS_OUTPUT.put_line('打印信息');
  11.    Elseif (判断条件) then
  12.      dbms_output.put_line('打印信息');
  13.    Else
  14.      Raise 异常名 (NO_DATA_FOUND);
  15.    End if;
  16.  Exception
  17.      When others then
  18.        Rollback;   
  19.  END;

2、基本使用

2.1、没有参数的过程

  1. create or replace procedure test_count 
  2.  is 
  3.   v_total int;
  4.   v_date varchar(20);
  5.  begin
  6.    select count(*) into v_total from dual;
  7.    select to_char(sysdate,'yyyy-mm-dd') into v_date from dual;   
  8.    DBMS_OUTPUT.put_line('总人数:'||v_total);  
  9.    DBMS_OUTPUT.put_line('当前时间:'||v_date);  
  10.  end;

调用方法

  1. begin
  2. test_count;
  3. end;

2.2、仅带传入参数的过程

  1. create or replace procedure test_count1(v_id in varchar2)
  2.  as
  3.   v_name varchar(100);
  4.  begin
  5.   select c_name into v_name from tb_store where c_stono=v_id;
  6.   DBMS_OUTPUT.put_line(v_id||'店的名称为:'||v_name);
  7.   exception
  8.    when no_data_found then dbms_output.put_line('no_data_found');
  9.  end;

调用方法

  1. begin
  2.  test_count1(11910);
  3. end;

2.3、仅带输出参数的过程

  1. create or replace procedure test_count2(v_name out varchar2)
  2.  is
  3.  begin
  4. select c_name into v_name from tb_store where c_stono='1101';
  5.   exception
  6.   when no_data_found then dbms_output.put_line('no_data_found');
  7.  end;

调用方法

  1. declare
  2. v_name varchar(200);
  3. begin
  4.   test_count2(v_name);
  5.   dbms_output.put_line(v_name);
  6. end;

2.4、带输入参数和输出参数的存储过程

  1. create or replace procedure test_count3(v_id in int,v_name out varchar2) 
  2.  as
  3.  begin  
  4. select c_name into v_name from tb_store where c_stono=v_id;
  5. dbms_output.put_line(v_name);
  6.   exception
  7. when no_data_found then dbms_output.put_line('no_data_found');
  8.  end;

调用方法

  1. declare
  2. v_name varchar(200);
  3. begin
  4.   test_count3('1101',v_name);
  5. end;

发表评论

表情:
评论列表 (有 0 条评论,75人围观)

还没有评论,来说两句吧...

相关阅读