• oracle存储过程


    --基本语法
    create or replace procedure hello
    as
    begin
    dbms_output.put_line('hello word');
    end;

    --执行
    1.--sqlplus命令
    execute
    exec

    2.begin .... end;

    3.call--sql命令

    exec hello();

    call hello();

    --带参数存储过程
    create or replace procedure money(eid in number)
    as
    oldmoney pro_test.money%type;--引用变量
    begin
    select t.money into oldmoney from pro_test t where t.id=eid;
    update pro_test t set t.money=t.money+100 where t.id=eid;
    dbms_output.put_line(oldmoney);
    end;
    call money(3);

    --存储函数
    create or replace function querymoney(eid in number)
    return number
    as
    oldmoney pro_test.money%type;
    oldname pro_test.name%type;
    begin
      select  t.name,t.money into oldname,oldmoney from pro_test t where t.id=eid;
      return nvl(oldmoney,0)*12;
    end;

    --out参数
    create or replace procedure queryone(eid in number,
                                         ename out varchar2,
                                         emoney out number)
    as
    begin
      select t.name,t.money into ename,emoney from pro_test t where t.id=eid;
    end;

    --包头
    create or replace package mypackage
    as
    --声明游标
    type mycursor is ref cursor;
    --定义存储过程
    procedure queryList(eid in number,eList out mycursor);
    end mypackage;

    --包体
    create or replace package body mypackage
    as
    procedure queryList(eid in number,eList out mycursor)
    as
    begin
      open eList for select * from pro_test;
    end queryList;
    end mypackage;

    --返回结果集
    create or replace procedure testList(eList out sys_refcursor)
    as
    begin
      open eList for select * from pro_test;
    end;

    --,综合
    create or replace procedure cursorTest(eList out sys_refcursor)
    as
    inta number;
    CURSOR cur_1 IS
      select * from pro_test;
    CURSOR cur_2   is
          select * from pro_test;
    cur_3 SYS_REFCURSOR;
    popo varchar2(100);
    begin
      FOR rec IN cur_1 LOOP
        DBMS_OUTPUT.put_line(rec.name);
       end loop;
      inta:=2;
      DBMS_OUTPUT.put_line(inta);
      if inta>2 then
        begin
          inta:=inta*10;
         end;
      elsif inta<=2 then
        begin
          inta:=inta*50;
          end;
       else
         begin
           DBMS_OUTPUT.put_line('判断结束');
         end;
       end if;
       DBMS_OUTPUT.put_line(inta);
       while inta<10000 loop
         inta:=inta+100;
       end loop;
       DBMS_OUTPUT.put_line(inta);
       if popo is null then
           DBMS_OUTPUT.put_line('popo is null');
       end if;
       if popo<>null then
          DBMS_OUTPUT.put_line('popo <> null');
       end if;
    end cursorTest;

    -----------------------------------------------------------------------
    实例

    下面写一个简单的例子来对以上所说的存储过程的用法做一个应用:

    现假设存在两张表,一张是学生成绩表(studnet) ,字段为:stdId,math,article,language,music,sport,total,average,step               

    一张是学生课外成绩表(out_school), 字段为:stdId,parctice,comment

    通过存储过程自动计算出每位学生的总成绩和平均成绩,同时,如果学生在课外课程中获得的评价为A ,就在总成绩上加20 分。

    create or replace procedure autocomputer(step in number) is

    rsCursor SYS_REFCURSOR;

    commentArray myPackage.myArray;

    math number;

    article number;

    language number;

    music number;

    sport number;

    total number;

    average number;

    stdId varchar(30);

    record myPackage.stdInfo;

    i number;

    begin

    i := 1;

    get_comment(commentArray); -- 调用名为get_comment() 的存储过程获取学生课外评分信息

    OPEN rsCursor for select stdId,math,article,language,music,sport from student t where t.step = step;

    LOOP

    fetch rsCursor into stdId,math,article,language,music,sport; exit when rsCursor%NOTFOUND;

    total := math + article + language + music + sport;

    for i in 1..commentArray.count LOOP  

     record := commentArray(i);     

    if stdId = record.stdId then   

     begin      

     if record.comment = 'A' then      

      begin          

     total := total + 20;    

       go to next; -- 使用go to 跳出for 循环        

      end;     

    end if;   

    end;   

    end if;

    end LOOP;

    <<continue>>  average := total / 5;

     update student t set t.total=total and t.average = average where t.stdId = stdId;

    end LOOP;

    end;

    end autocomputer;

    -- 取得学生评论信息的存储过程

    create or replace procedure get_comment(commentArray out myPackage.myArray) is

    rs SYS_REFCURSOR ;

    record myPackage.stdInfo;

    stdId varchar(30);

    comment varchar(1);

    i number;

    begin

    open rs for select stdId,comment from out_school

    i := 1;

    LOOP

     fetch rs into stdId,comment; exit when rs%NOTFOUND;

    record.stdId := stdId;

     record.comment := comment;

    recommentArray(i) := record;

    i:=i + 1;

    end LOOP;

    end get_comment;

    -- 定义数组类型myArray

    create or replace package myPackage is begin

    type stdInfo is record(stdId varchar(30),comment varchar(1));

    type myArray is table of stdInfo index by binary_integer;

    end myPackage;

    JDBC调用存储过程

    CallableStatement cs = conn.prepareCall("{call all_user()}");

      cs.setInt(1, 3);

      cs.registerOutParameter(2, Types.CHAR);

      cs.execute();

      String name =cs.getString(2);

    hibernate调用存储过程

    HibernateSessionFactory.getSession().createSQLQuery("{Call proc()}");  List list =query.list();  
    session.connection();  CallableStatement cs = conn.prepareCall("{call all_user()}");

    jpa调用存储过程

      entityManager.createNativeQuery

      注解@NamedStoredProcedureQueries

      

    //假如我要获取一个User的集合,这个存储过程名字是GetUserList()
    Query query = em.createNativeQuery("{call GetUserList()}",User.class);
    List<User>result=query.getResultList();
    //result就是你要的全部user
     
     
  • 相关阅读:
    PAT 甲级 1129 Recommendation System
    PAT 甲级 1129 Recommendation System
    PAT 甲级 1128 N Queens Puzzle (20 分)
    PAT 甲级 1128 N Queens Puzzle (20 分)
    PAT 甲级 1096 Consecutive Factors (20 分)
    PAT 甲级 1096 Consecutive Factors (20 分)
    PAT 甲级 1135 Is It A Red-Black Tree (30 分)
    PAT 甲级 1135 Is It A Red-Black Tree (30 分)
    AcWing 900. 整数划分
    AcWing 899. 编辑距离 线性dp
  • 原文地址:https://www.cnblogs.com/bigmonkeys/p/7891400.html
Copyright © 2020-2023  润新知