• Oracle之子程序(存储过程、方法、包)


    1.过程【存储过程】
        CREATE [OR REPLACE] PROCEDURE 
           <procedure name> [(<parameter list>)]
        IS|AS 
           <local variable declaration>
        BEGIN
           <executable statements>
        [EXCEPTION
           <exception handlers>]
        END;
        
        1.1无参存储过程
             写一个存储过程:实现向student表中插入10条记录的功能
        create or replace procedure  pro_student
        is 
         --声明变量
        begin
         -- 执行逻辑代码
            for i in 950..980 loop
               insert into student(id,name) values(i,'asasas');
               commit;
            end loop;
             --异常处理
        end;
        
        --注意:选中过程代码按齿轮提交并不是执行代码而是编译代码并保存在数据库中
                -- 保存在左侧菜单栏中的 procedures目录中
       调用存储过程:
               1.选中过程名称右键‘测试’选项执行
               2.通过plsql块调用
               
               begin
                   pro_student();--如果没有参数 ()可以省略
               end;
        1.2 有参存储过程
        create or replace procedure pro_student2(
            p_id in number,-- 默认就是in 可以省略
            p_name in varchar2, --没有被out修饰的参数是常量,不能被改变
            p_result out student%rowtype   -- 接收返回信息
    
        )
        is 
        begin
           select * into p_result from student where id=p_id and name=p_name;
        end;
    
    
        declare 
            v_id student.id%type := '&请输入id';
            v_name student.name%type :='&请输入姓名';
            v_student student%rowtype;
        begin
            pro_student2(v_id,v_name,v_student);
            dbms_output.put_line(v_student.id);
            dbms_output.put_line(v_student.name);
            dbms_output.put_line(v_student.sex);
            dbms_output.put_line(v_student.birth);
            dbms_output.put_line(v_student.address);
                 
        end;
        
        
        
        
        案例:请根据性别或名字查询相关记录,并把结果 返回来 打印了出来  提示用 sys_refcursor
        
        create or replace procedure pro_student3(
             v_name in student.name%type,
             v_sex in student.sex%type,
             mycursor out sys_refcursor
        )
        is 
        v_sql varchar2(100);
        begin
         
          v_sql:='select * from student where 1=1';
          if v_name is not null then
             v_sql := v_sql || 'name like ''%'||v_name||'%'''; 
          end if;
          if v_sex is not null then
             v_sql := v_sql || 'sex = '''||v_sex||''''; 
          end if;
          dbms_output.put_line(v_sql);
          open mycursor for v_sql;
          close mycursor;
        end;
        
        
        
        declare 
             v_cursor sys_refcursor;
             v_name student.name%type:='&请输入姓名';
             v_sex student.sex%type:='&请输入性别';
             v_student student%rowtype;
        begin
            pro_student3(v_name,v_sex,v_cursor);
            
            loop
              fetch v_cursor into v_student;
            exit when v_cursor%notfound;
            dbms_output.put_line(v_student.id||v_student.name);
            end loop;
            close v_cursor;
        end;
        
       
       
     2.函数【方法】:有显示的返回值
        CREATE [OR REPLACE] FUNCTION 
            <function name> [(param1,param2)]
          RETURN <datatype>  IS|AS 
            [local declarations]
          BEGIN
            Executable Statements;
            RETURN result;
          EXCEPTION
            Exception handlers;
          END;
     写一个方法实现根据班级编号获取班级名称
       create or replace function func_class(
          f_id number
       )return varchar2
       is
         v_name t_class.cname%type;
       begin
        select cname into v_name from t_class where cid = f_id;
        return v_name;
        
        exception
        when no_data_found then 
           dbms_output.put_line('找不到数据');
           return null;
        when others then 
         dbms_output.put_line('其他异常');
          return null;
       end;
       
       
         调用方法:
          1.通过工具测试
          2.通过plsql块执行
             begin
               dbms_output.put_line(func_class(1));
               end;
          3.通过SQL语句调用【方法中不能有DML操作】
            select fun_test1(102) from dual;
            select t.*,fun_test1(t.classid) from t_student t
    
        
        
        
        

    方法和过程的区别:
    1.DML相关的操作我们一般都使用存储过程实现
    2.特定的公共的功能我们用方法实现
    3.方法有显示的返回结果
    4.方法中同样的也有 in out 关键字

    create or replace package p_student is
    
      -- Author  : BOBO
      -- Created : 2018-08-06 11:29:41
      -- Purpose : 
      
      -- Public type declarations  定义一个类型
      --type <TypeName> is <Datatype>;
      --type myreftype is ref cursor;
      -- Public constant declarations -- 声明一个常量
      --<ConstantName> constant <Datatype> := <Value>;
    
      -- Public variable declarations
      --<VariableName> <Datatype>; -- 声明一个变量
    
      -- Public function and procedure declarations
      --function <FunctionName>(<Parameter> <Datatype>) return <Datatype>;
    
      procedure insert_student(
        p_id t_student.id%type,
        p_name t_student.name%type
        );
      
      function getname(
        p_id t_student.id%type
        )return varchar2;
    
    end p_student;
    
    create or replace package body p_student is
    
      -- Private type declarations
      --type <TypeName> is <Datatype>;
      
      -- Private constant declarations
      --<ConstantName> constant <Datatype> := <Value>;
    
      -- Private variable declarations
      --<VariableName> <Datatype>;
    
      -- Function and procedure implementations
     -- function <FunctionName>(<Parameter> <Datatype>) return <Datatype> is
        --<LocalVariable> <Datatype>;
     -- begin
        --<Statement>;
        --return(<Result>);
     -- end;
    
    --begin
      -- Initialization
      --<Statement>;
      
      procedure insert_student(
        p_id t_student.id%type,
        p_name t_student.name%type
        )is
        begin
          insert into t_student(id,name)values(seq_t_student.nextval,p_name);
          commit;
        end;
      
      function getname1(
        p_id t_student.id%type
        )return varchar2
        is
          v_name t_student.name%type;
        begin
          select name into v_name from t_student where id = p_id;
          return v_name;
        end;
      
      function getname(
        p_id t_student.id%type
        )return varchar2
        is
          v_name t_student.name%type;
        begin
          select name into v_name from t_student where id = p_id;
          return getname1(p_id);
        end;
        
       
       
    end p_student;

    不在package中声明,直接在body中定义实现,是为了隐藏方法,同时给内部的其他方法或者存储过程调用

  • 相关阅读:
    1070 结绳
    1069 微博转发抽奖
    1068 万绿丛中一点红
    1067 试密码
    1066 图像过滤
    1065 单身狗
    CSS--文本溢出与换行
    css--滤镜filter
    css--flex布局
    css--table布局
  • 原文地址:https://www.cnblogs.com/lrxvx/p/9432874.html
Copyright © 2020-2023  润新知