zoukankan      html  css  js  c++  java
  • Oracle--存储过程学习进阶

    例1:该存储过程是向xuesheng 表中插入一行数

    1 create or replace procedure student_proc_no is
    2 begin
    3 insert into Student(id,name,C语言,软件工程) values (3, 'wangwu', 90, 90);
    4 commit;
    5 end student_proc_no;

    例2:带游标的存储过程,遍历游标将数据存储到另一个表中

     1 create or replace procedure Score  --新增学分存储过程
     2 is
     3 begin
     4   declare
     5     cursor CourseCursor is --建立游标
     6         select  Id,
     7                 name,
     8                 score,
     9         from wangou.Course;
    10     c_row CourseCursor%rowtype;
    11   begin
    12     for c_row in CourseCursor loop 遍历上面建立的游标
    13     
    14       ----插入对账单信息
    15       insert into ScoreList
    16       (                 
    17         id,
    18         CourseName,
    19         score,
    20       )
    21       values
    22         (s_ScoreList.Nextval,
    23          c_row.name,
    24          c_row.score
    25         );
    26    
    27     end loop;
    28   end;
    29 End;
    30

    例3: 存储过程实例:该存储过程的功能是:查询Student表中Status状态等于0的数据,之后一一插入到StudentCourseList表中,每插入一条记录之后将状态改为1;其中插入的过程中需要根据游标中的数据查询到其他表中相关数据插入到StudentCourseList表中。

     1 create or replace procedure StudentCoursePro  --新增学生课程存储过程
     2  is
     3   TmpCourseId number(9);--变量
     4   TmpCourseName varchar2(50);--变量
     5   Tmpdirection int;--变量
     6 begin
     7   declare
     8     cursor StudentCursor is --建立游标
    10         select  Id,
    11                 StudentCode,
    12                 substr(CourseCode, -5) as CourseCode,--截取查询到的字段,负数从最右边开始截取,5代表截取位数为
                                     --5位。-5:代表从左右边开始向前(向左)截取5位数
    13 Direction,
               createtime,
    14 Status 15 from wangou.Student 16 where Status = 0 17 for update of Status; 18 c_row StudentCursor%rowtype; 19 begin 20 for c_row in StudentCursor loop 遍历上面建立的游标 21 --查询课程信息 22 select id, 23 Name 24 into TmpCourseId, 25 TmpCourseName 26 from Course 27 where code = c_row.CourseCode;--c_row.字段:为表中某一行的该字段的值 28 29 -- 30 if (c_row.direction='A') then --当direction等于‘A’时,Tmpdirection=1 31 Tmpdirection:=1; 32 else                --当direction不等于‘A’时,Tmpdirection=2 33 Tmpdirection:=2; 34 end if; 35 36 ----生成学生课程列表信息 37 insert into wangou.StudentCourseList--注意该表的ID为自动oracle的sequence序列号,因为该表是在另一个方案名下, 38 (--id,                 --无法引用到sequence,所以在另一个方案下提前建立了触发器,代码在文章末尾处给出 39 Name, 40 CourseID, 41 CouseName, 42 TearcherId, 43 direction, 44 date, 45 46 ) 47 values 48 (--s_StudentCourseList.Nextval, 49 Name,TmpCourseID,50 TmpCourseName, 51 (select id from Tearcher where StudentID=c_row.ID),--查找老师信息 52 Tmpdirection, 53 to_date(c_row.createtime,'yyyy-mm-dd hh24:mi:ss'),--将createtime时间转换为需要的时间格式 54 55 56 ); 57 58 ----更新对账单.处理状态 = 1(已处理)。 59 update wangou.Student    --wangou为方案名 60 set Status = 1 61 WHERE CURRENT OF StudentCursor; 62 end loop; 63 end; 64 End; 65

     触发器代码:

    该触发器的功能是:当有数据插入到StudentCourseList表中之前,为该表的ID(即“:new.id”)预先给个值,

             值即为该表的序列号S_StudentCourseList.NEXTVAL

     1 create or replace trigger StudentCourseListTri
     2   before insert on StudentCourseList --当有数据插入StudentList表之前
     3   for each row
     4 declare
     5   nextid NUMBER;--变量
     6 begin
     7   SELECT S_StudentCourseList.NEXTVAL
     8   INTO nextid
     9   FROM dual;
    10 :new.id:=nextid;
    11 end StudentCourseListTri;
  • 相关阅读:
    vue cli3 打包到tomcat上报错问题
    前端html转pdf
    原生js上传图片遇到的坑(axios封装)
    vue slot的使用(transform动画)
    vue购物车动画效果
    关于el-select 单选与多选切换的时候报错的解决办法
    vue html属性绑定
    关于element ui滚动条使用
    css3flex布局实现商品列表 水平垂直居中 上下布局
    vue 项目 路由 router.push、 router.replace 和 router.go
  • 原文地址:https://www.cnblogs.com/wangoublog/p/3761773.html
Copyright © 2011-2022 走看看