文档详情

11编程进阶_PLSQL.ppt

发布:2017-05-07约字共73页下载文档
文本预览下载声明
学习目标: 通过实例的方式让大家掌握PL/SQL的编程基本要素,从而完成复杂的管理任务。 PL/SQL程序结构 基本语法要素 流程控制 事务处理 游标 异常处理 过程 自定义函数 包 触发器 DDL触发器语法 系统事件触发器语法 登录/退出触发器 使用call的触发器 使用触发器控制完整性约束条件 使用触发器控制完整性约束条件 练习一:记录变量的使用 定义一个scott.student表的记录类型变量,把student表中的学生胡青牛的记录读取到记录变量并输出记录的姓名和专业信息。 步骤: 1、定义一个student表记录变量; 2、使用select into语句将学生胡青牛的记录读入记录变量; 3、输出记录变量的字段值。 set serveroutput on declare myrecord scott.student%rowtype; begin select * into myrecord from scott.student where name=胡青牛; dbms_output.put_line(|| ||fessional); end; 练习二:多维表变量的使用 .定义一个多维表变量,表的元素类型与scott.student表的记录类型一致,要求把student表中姓名为胡青牛的记录和姓名为萧峰的记录读取到表变量中,然后输出表变量的元素记录的学生姓名和专业信息。 步骤: 1、定义一个多维表类型,其元素类型为记录类型; 2、使用select into语句将学生记录读入多维表变量的元素中; 3、输出多维表元素的学生姓名和学生专业信息。 set serveroutput on declare type tabletype is table of scott.student%rowtype index by binary_integer; mytable tabletype; i number :=1; begin select * into mytable(i) from scott.student where name=萧峰; i:=i+1; select * into mytable(i) from scott.student where name=丁敏君; i:=mytable.first; while i0 loop dbms_output.put_line(i); dbms_output.put_line(mytable(i).name|| ||mytable(i).professional); i:=mytable.next(i); end loop; end; set serveroutput on declare tempsal scott.emp.sal%type; cursor mycursor is select * from scott.emp where saltempsal; begin tempsal:=800; open mycursor; end; //以scott用户连接数据库,在【SQLPlus Worksheet】中执行PL/SQL程序,该程序定义tempsal为与scott.emps数据表中的sal字段类型相同的变量,mycursor为从scott.emp数据表中提取的sal大于tempsal的数据构成的游标。 fetch 游标名 into 变量名1, 变量名2,……; set serveroutput on declare v_name %type; v_loc varchar2(20); cursor mycursor is select dname,loc from scott.dept; begin open mycursor; fetch mycursor into v_name,v_loc; dbms_output.put_line(v_name|| ||v_loc); end; fetch 游标名 into 游标名%ROWTYPE; set serveroutput on declare tempsal scott.emp.sal%type; cursor mycursor is select * from scott.emp where saltempsal; cursorrecord mycursor%rowtype; begin tempsal:=800; open
显示全部
相似文档