




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、一、 pl/sql基本语法要素1. 相对其他语言特殊运算符=号是是否相等的比较运算符:=才是赋值运算符 两个单引号之间表示字符类型的变量 双引号之间标识引用,如果字段名,数据库名等| 表示字符之间的连接符-表示单行注释%属性指示器2. pl/sql变量及数据类型1. 变量类型标量类型:数值,字符,日期,布尔复合类型:索引表,嵌套表,数组引用类型:游标类型(cursor),对象类型(ref)lob类型:大文本等2. 使用%定义变量的类型跟某个表的列属性一致的变量的声明 v_name %type 表示v_name 这个变量与表student中的列name 属性一样v_stu
2、dent student%rowtype 表示v_student这个变量客户存student中的一行数据3. 用select into个变量赋值的时候select结果必须只有一行数据,如果多行会包too many row错误,空的话则会报空错误。3. pl/sql中最基本的语句块结构declare 声明变量,要使用的变量必须在这里声明begin 需要执行的程序语句exception异常处理语句end二、 pl/sql 程序顺序结构(分支和循环)1. 正常都是从上到下的顺序结构2. 使用if进行分支处理if 语句的格式:if condition_1 then statement_1; elsif
3、 condition_n then statement_n;else else_statements; end if;3. 使用case进行分支处理case语句格式:1. 单值比较: case expression when result_1 then statements_1;when result_2 then statements_2;else else_statement;end case;2. 多值比较: case when expression_1 then statement_1;when expression_2 then statement_2;else else_state
4、ments;end case;4. 使用循环结构1. 使用loop循环结构:loop statements;exit when expression;end loop;满足expression条件的时候跳出循环;2. 使用while循环结构:while expression loop statements;end loop;expression 的格式:vi=10 不加分号3. 使用for循环结构:for expression loop statements;end loop;expression 的格式:vi in 1.10 不叫分号三、 游标基础及使用1. 游标的定义:游标用于表示一查询结
5、果的结果集2. 游标使用四个步骤:-1.声明declare - 2.打开 open -3.读取fetch -4.关闭close1. 声明游标:cursor cursor_name is select_statements;例如:cursor cursor_student is select id,name,age,sex from students where name like 林%;2. 使用游标是打开:open cursor_name;例如:open cursor_student;打开游标的时候系统才会去查询游标声明的数据集;3. 读取数据:fetch cursor_name into
6、variable_name1,variable_name2;例如:fetch cursor_studnet into v_id,v_sname,v_age,v_sex;第一次读取的是第一行数据,再次使用fetch读取数据的时候是下一行所以处理游标时一般都是用循环;4. 关闭游标:close cursor_name;例如:close cursor_student;3. 游标中的属性:四个常用属性:%isopen 、 %found 、 %notfound 、 %rowcount1. %isopen 返回游标是否打开 使用方法 cursor_name%isopen 例如:cursor_student
7、%isopen;2. %notfund 返回最近一次fetch取数是否有取到数,没取到返回true,有取到返回false;一般用于判断循环终止使用方法:cursor_name%notfund 例如:cursor_student%notfound3. %found 与%notfound相反4. %rowcount 返回现在已经读取的行数使用方法:cursor_name%rowcount例如:cursor_student%rowcount4. 简单使用游标循环读取数据的例子读取并打印学生表中“计算机系学生的信息”declarev_specialty students.specialty%type;
8、 -声明变量v_name %type;v_dob students.dob%type;cursor student_cur -声明游标is select name,dob from students where specialty=v_specialty;begin v_specialty:=计算机; -给变量赋值open student_cur; -打开游标dbmes_output.put_line(学生姓名 出生日期); -输出loop fetch student_cur into v_name,v_dob; -读取游标中的数据exit when student_
9、cur%notfund; -设定退出条件dbms_output.put_line(v_name| |v_dob); -输出结果end loop; -循环体结束标志close sutent_cur; -关闭游标end; -块结构结束标志5. 游标的应用:1. 对游标指定的数据进行修改:需要对游标指定的数据修改必须在声明游标的时候带有for update 关键字语法:cursor cursor_name is select_statement for update of table_name notwait;of table_name 可以不写,用于自动选择多表的时候确定那些表需要锁定;notwa
10、it可以不写,用于确认是否等待锁。对数据操作的语句使用where current of 关键字表示修改游标指向的当前行。例如根据职称调整教师工资:declarev_title teachers.title%type;cursor teachers_cur -声明游标并指定为可修改isselect title from teachers for update;begin open teachers_cur; -打开游标 loop fetch teachers_cur into v_title; -读取游标数据 exit when teacher_cur%notfound; -退出循环条件 cas
11、e -使用case分支判断 when v_title=教授 then update teachers set wage=1.1*wage where current of teachers_cur; -使用where current of 指定修改的当前行 when v_title=高工 then update teachers set wage=1.05*wage where current of teachers_cur; end case; -结束case分支end loop;close teachers_cur;end;2. 使用游标对数据进行删除跟修改类似,参考修改的例子,同样需要使
12、用 where current of 关键字。3. 游标while循环open student_curfetch student_cur into v_name,v_specialty;while student_cur%found loop if v_specialty=计算机 then delete from students where current of students_cur; end if; fetch student_cur into v_name,v_specialty;end loop;close student_cur;4. 游标for循环使用for循环不用使用fetc
13、h 和close 游标格式一:(使用声明的游标)for record_name in cursor_name loop statement1; statement2;end loop;格式二:(使用子查询(匿名游标)for record_name in selectstatement loop statement1; statement2;end loop;例如显示计算机系所有学生信息:格式一:declare v_specialty students.specialty%type; cursor students_cur is select name,dob from students whe
14、re specialty=v_specialty; begin v_specialty:=计算机; dbms_output.put_line(序号 学生姓名 出生日期); for student_record in students_cur loop dbms_output.put_line(students_cur%rowcount| |student_| |student_record.dob); end loop; end;格式二:declare v_specialty students.specialty%type; begin v_specialty:=计算机;
15、 dbms_output.put_line(学生姓名 出生日期); for student_record in (select name,dob from students where specialty=v_specialty) loop -直接使用只查询 dbms_output.put_line( |student_| |student_record.dob); end loop; end;5. 使用游标参数:(给游标传递参数)语法格式:cursor cursor_name(para_name1 datatype,para_name2 datatype,) is se
16、lect_statement;例如用计算机系作为参数显示计算机系的学生信息:declare v_name students.name%type; v_dob students.dob%type; cursor students_cur(v_specialty students.specialty%type) is select name, dob from students where specialty = v_specialty;begin open students_cur(计算机系);-打开游标的时候使用计算机系作为参数 fetch students_cur into v_name,
17、v_dob; while students_cur%found loop dbms_output.put_line(v_name | | v_dob); fetch students_cur into v_name, v_dob; end loop; close students_cur;end;6. 游标变量游标变量可以指定动态的变更指定不同的游标。游标变量的使用也要经过四个步骤:1.定义 2.打开 3.读取数据 4.关闭数据1. 定义游标变量:(1).定义游标类型type ref_type_name is ref cursor return return_type;ref_type_nam
18、e 指定游标变量使用的数据类型;return return_type 为ref_type_name指定返回的类型,可以省略,而且类型必须是record类型的数据;没有指定return的时候游标变量能跟任何select语句关联;(2).定义游标类型变量:cursor_variable ref_type_name;cursor_variable 游标变量名;2. 打开游标变量:open cursor_variable for select_statement;select_statement 查询子句3. 读取游标变量的数据:fetch cursor_variable into variable1
19、,variable2,.;4. 关闭游标变量:close cursor_variable;7. 游标变量使用实例1. 显示学生姓名和出生日期,不实用returndeclare type student_cur is ref cursor; -定义游标类型stucursor student_cur; -声明游标变量students_record students%rowtype;begin if not stucursor%isopen then open stucursor for select * from students;-打开游标变量并附select子句 end if; dbms_ou
20、tput.put_line(学生姓名 出生日期); loop fetch stucursor into students_record; -读取游标变量数据 exit when stucursor%notfound; dbms_output.put_line(students_| |students_record.dob); end loop; close stucursor; -关闭游标end;2. 显示学生姓名和出生日期,使用returndeclare type students_record is record( -定义一个记录类型的数据 stuname varch
21、ar2(10), studob date ); sturecord students_record; -声明一个记录type students_cur is ref cursor return students_record; -定义一个游标类型并且游标类型的数据是上面的记录类型 stucursor students_cur; -声明一个游标变量begin if not stucursor%isopen then open stucursor for select name,dob from students; -打开游标select子句要和record一致 end if; dbms_outp
22、ut.put_line(学生姓名 出生日期); loop fetch stucursor into sturecord; exit when stucursor%notfound; dbms_output.put_line(sturecord.stuname| |sturecord.studob ); end loop; close stucursor; end;8. 游标表达式游标表达式在select语句中使用,构成嵌套游标。(在声明游标时候的select语句中使用),返回是是游标变量(ref cursor)。游标表达式的格式:cursor(subquery)例子:循环显示个系部的所有老师姓
23、名和职称declare v_tname %type; v_title teachers.title%type; v_dname departments.department_name%type; type cursor_type is ref cursor; -定义一个游标类型 -声明一个游标,并且一个游标表达式嵌套 cursor departments_cur(dept_id number) is select d.department_name,cursor(select name,title from teachers where department_id=d
24、.department_id) from departments d where d.department_id=dept_id; teachers_cur cursor_type; -声明一个游标变量begin open departments_cur(101); -打开外层游标 loop fetch departments_cur into v_dname,teachers_cur; -取外层游标数据同时把内层游标打开 exit when departments_cur%notfound; dbms_output.put_line(系部名称:|v_dname); dbms_output.p
25、ut_line(教师名 职称); loop fetch teachers_cur into v_tname,v_title; -取内层游标数据 exit when teachers_cur%notfound; dbms_output.put_line(v_tname| |v_title);end loop;end loop;四、 复合数据类型1. 记录类型显示的定义记录类型格式:type record_type_name is record( -定义一个记录类型filed1_name datatype1, -记录类型的成员 名称和数据类型filed2_name datatype2,filed3
26、_name datatype3);variable_name record_type_name; -声明一个记录变量2. 记录表类型数据记录类型的数据类似二维数组定义记录表类型的格式:type recordtable_type_name is table of table_name%rowtype| column_name%type index by binay_integer; -定义记录表variable_name recordtable_type_name; -声明记录表table_name%rowtype跟column_name%type 为二选一指定一行数据中包含的变量binay_i
27、nteger 指定表元素下标使用的数据类型使用记录表是要指定列variable_name(index_number)如:variable_name(2) 表示下表为2的记录type student_recordtable is table of students%rowtype index by binay_integer;select * into student_recordtable(1) from students where id=101;五、 子程序1. 存储过程定义存储过程的语句:create or replace procedure procedure_name (argume
28、nt_list in |out | in out argument_type,)is | asvariable_name1 datatype1; -声明变量variable_name2 datatype2;beginprocedure_body;end procedure_name;or replace 可加可不加,加的话会覆盖相同名称的存储过程argument_list 表示参数列表argument_type 表示参数类型in |out | in out 三选一 in 表示外部输入给过程,out表示参数在过程中可以被赋值is | as 二选一variable_name1 datatype1
29、声明变量和变量类型调用或执行存储过程:call| execute procedure_name(argument_list);例如:显示某系的老师的平均工资,最大工资,最小工资create or replace procedure display_teacher(v_no teachers.department_id%type)as v_wage teachers.wage%type; v_maxwage teachers.wage%type; v_minwage teachers.wage%type;begin select avg(wage) into v_wage from teache
30、rs where department_id=v_no; select max(wage) into v_maxwage from teachers where department_id=v_no; select min(wage) into v_minwage from teachers where department_id=v_no; dbms_output.put_line(该系平均工资:|v_wage); dbms_output.put_line(该系最大工资:|v_maxwage); dbms_output.put_line(该系最低工资:|v_minwage);exceptio
31、n when no_data_found then dbms_out.put_line(该系不存在。);end display_teacher;存储过程管理:在系统里面查找存储过程 select object_name,created,status from user_objects where object_name=procedure_name查看存储过程代码:select text from user_source where name=procedure_name2. 函数函数与过程的不同在于函数可以换回值,过程不能有返回值函数定义格式:create or replace functi
32、on function_name (argument_list in |out | in out argument_type,)return datatype -返回数据类型is | asvariable_name1 datatype1; -声明变量variable_name2 datatype2;begin procedure_body; return expression; -换回语句end procedure_name;调用函数方法:直接用表达式:function_name(argument_list)函数管理:查找函数:select object_name,created,status
33、 from user_objects where object_name=function_name;查看函数代码:select text from user_source where name=function_name;3. 触发器触发器是特殊的子程序,不能被调用。只能被触发。由select insert update delete 等dml操作触发触发关键字after 和 before 之后和之前触发触发器格式:create or replace trigger trigger_name before | after insert or update or delete of colum
34、n_name on table_namebegin trigger_statement1; raise_application_error(-20000, error message); -定义触发器执行时检测条件不符合时的错误 end trigger_name;before | after 二选一 before就是在触发事件前触发insert or update or delete 可以一个或者多个column_name 表示修改对应的列时触发 可不写表示对整张表简单的触发器实例:非工作时间无法修改教师信息:create or replace trigger change_teacher b
35、efore insert or update or delete on teachersbegin if to_char(sysdate, hh24) not between 8 and 17 or to_char(sysdate, dy, nls date_langudage=american) in (sat, sun) then raise_application_error(-20000, 非工作时间不能改变教师信息!); end if end change_teacher;触发器管理:查询系统中的触发器:select * from user_triggers where trigger_name=trigger_name;启用,禁用,删除触发器alter trigger trigger_name enable;alter trigger trigger_name disable;drop trigger trigger_name;4. 创建临时表:会话级的临时表:create global temporary table table_name (col1 type1,col2 type2.) on commit preserve rows;例:create global temporary table
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中介押金合同范本
- 2025年漳州货运准驾证模拟考试
- 医院器械采购合同范本
- 加工类协议合同范本
- 办公窗帘购销合同范本
- 村级采购合同范本
- 代销铺货合同范本
- 买卖合同和货运合同范本
- 专利转让英文合同范例
- 北京不备案施工合同范本
- 专题13《竹里馆》课件(共28张ppt)
- 团意操作流程详解课件
- SH/T 0356-1996燃料油
- GB/T 9846.4-2004胶合板第4部分:普通胶合板外观分等技术条件
- GB/T 17836-1999通用航空机场设备设施
- GB/T 13012-2008软磁材料直流磁性能的测量方法
- 2023年全国高中生物联赛竞赛试题和答案
- 第1课中华优秀传统文化的内涵与特点课件(共28张PPT)
- 小学语文中高学段单元整体教学的实践研究课题中期报告
- 《木兰诗》第二课时(公开课)课件
- 核电项目人桥吊车抗震计算书版
评论
0/150
提交评论