`
chenhua_1984
  • 浏览: 1234556 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

PLSQL开发基础--结构控制

阅读更多
--匿名块
set serveroutput on;
begin
    dbms_output.put_line('this is an anonymous block');
end;
/ 

--不考虑无数据异常
declare
   v_sname varchar2(200);
begin
		select name into v_sname from students t where t.student_id=10381;
		dbms_output.put_line('name is'||v_sname);
end;
/

--捕获空异常
declare
   v_sname varchar2(200);
begin
		select name into v_sname from students t where t.student_id=10381;
		dbms_output.put_line('name is'||v_sname);
		exception
			when no_data_found then
			   dbms_output.put_line('no data found');			   
end;
/

--注释的例子,此例子包含CASE语法,%数据类型,&表示输入
declare
	v_id teachers.teacher_id%type;
	v_job_title teachers.title%type;
begin
	v_id:=&teacher_id;
	select title into v_job_title from teachers t where t.teacher_id=v_id; --将教师标号为V——id的教师职称赋值给变量
	
	case
	  when v_job_title='教授' then
	    update teachers set wage=1.1*wage where teacher_id=v_id;
	  when v_job_title='高级工程师' or v_job_title='副教授' then
	    update teachers set wage=1.05*wage where teacher_id=v_id;
	  else
	    update teachers set wage=wage+100 where teacher_id=v_id;
	  end case;
end;
/



--注释的例子,此例子包含CASE语法,%数据类型,异常处理
declare
	v_id teachers.teacher_id%type;
	v_job_title teachers.title%type;
begin
	v_id:=&teacher_id;
	select title into v_job_title from teachers t where t.teacher_id=v_id; --将教师标号为V——id的教师职称赋值给变量
	exception 
			when no_data_found then
			dbms_output.put_line('no teachear be found');
	case
	  when v_job_title='教授' then
	    update teachers set wage=1.1*wage where teacher_id=v_id;
	  when v_job_title='高级工程师' or v_job_title='副教授' then
	    update teachers set wage=1.05*wage where teacher_id=v_id;
	  else
	    update teachers set wage=wage+100 where teacher_id=v_id;
	end case;
end;
/

--假如只有1个值,那么可以这样写,假如比较的条件比较多,那么换上面的写法
declare
	v_id teachers.teacher_id%type;
	v_job_title teachers.title%type;
begin
	v_id:=&teacher_id;
	select title into v_job_title from teachers t where t.teacher_id=v_id; --将教师标号为V——id的教师职称赋值给变量
	exception 
			when no_data_found then
			dbms_output.put_line('no teachear be found');
	case v_job_title
	  when '教授' then
	    update teachers set wage=1.1*wage where teacher_id=v_id;
	  when '高级工程师' then
	    update teachers set wage=1.05*wage where teacher_id=v_id;
	  else
	    update teachers set wage=wage+100 where teacher_id=v_id;
	end case;
end;
/


--在PLSQL中执行select语句,
select * from departments;
declare
		v_id departments.department_id%type;
		v_name departments.department_name%type;
		v_address departments.address%type;
	begin
	    select * into v_id,v_name,v_address from departments where department_id=&department_id;
			
			dbms_output.put_line('系部名称:'||v_name);
			dbms_output.put_line('系部地址:'||v_address);
end;
/


--行类型的数据类型 ,异常处理应该写到select into 之后?还是输出之后?还是只要有异常处理,写到哪里都没有关系,oracle会自动需找异常处理的代码?
--假如有些异常不可预料,怎么写?
declare
		v_student students%rowtype;
		begin
		 select * into v_student from students where student_id =&student_id;
		 dbms_output.put_line('name sex birthday');
		 dbms_output.put_line(v_student.name || v_student.sex||v_student.dob);
		 exception 
		    when no_data_found then
		      dbms_output.put_line('no data found');
		 /*在表中执行select into 这条语句,如果返回值为空,那么会抛出no_data_found异常,
		 
		 如果返回的结果过多,那么则会抛出too_many_row异常*/		 
		end;		
/

--假如没有找到数据,什么都不做
declare
		v_id teachers.teacher_id%type;
		v_title teachers.title%type;
begin
		v_id:=&teacher_id;
		select title into v_title from teachers where teacher_id=v_id;
		exception 
				when no_data_found then
				null;
		if v_title='' then
				update teachers set wage=1.1*wage where teacher_id=v_id;		
		else
				update teachers set wage=wage+100 where teacher_id=v_id;
		end if;
end;

---多次elsif的例子
declare
		v_id teachers.teacher_id%type;
		v_title teachers.title%type;
begin
		v_id:=&teacher_id;
		select title into v_title from teachers where teacher_id=v_id;
		exception 
				when no_data_found then
				null;
		if v_title='' then
				update teachers set wage=1.1*wage where teacher_id=v_id;		
		elsif v_title='aaa' then
		
				update teachers set wage=wage+100 where teacher_id=v_id;
		elsif  v_title='ccc' then
				null;
		else
				null;
		end if;
end;
--loop循环的例子
create table total(n int ,reault int);

declare
		v_i int:=1;
		v_sum int:=0;
begin
		loop
		v_sum:=v_i+1;
		insert into total values (v_i,v_sum);
		
		exit when v_sum=10;
		v_i:=v_i+1;
		end loop;
end;
/

select * from total;

--while 循环的例子
declare
		v_i INTEGER:=1;
		v_sum INTEGER:=0;
begin
		while v_i<=10 loop
			v_sum:=v_i+1;
			insert into total values(v_i,v_Sum);
			v_i:=v_i+1;
			end loop;
			commit;
end;
/
select * from total;

--for循环的测试例子

declare
	v_i int:=1;
	v_sum int :=1;
begin
	for v_i in 1..100 loop
		v_sum:=v_i*1;
	  insert into total values (v_i,v_sum);
	end loop;
	commit;
end;
/
select * from total;
 
分享到:
评论

相关推荐

    ORACLE 数据库开发_PLSQL基础.doc

    大量源码案例,手把手教你PLSQL数据库开发。内容预览: ---- 第一章 PL/SQL 简介 ---- ---- 第二章 PL/SQL程序结构 ---- ---- 第三章 变量与数据类型 ---- ---- 第四章 PL/SQL控制语句 ---- ---- 第五章 PL/SQL游标 ...

    Oracle_PLSQL语言基础

    PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用PL/SQL,本文将讲述PL/SQL基础语法,结构和组件、以及如何设计并执行一个PL...

    PLSQLDeveloper下载

    3) 顺序结构 实际就是goto的运用,不过从程序控制的角度来看,尽量少用goto可以使得程序结构更加的清晰。 变量声明与赋值  PL/SQL主要用于数据库编程,所以其所有的数据类型跟Oracle数据库里的字段类型是一一对应...

    pl/sql 开发详解

    从Oracle6开始,Oracle公司在标准SQL的基础上发展了自己的PL/SQL语言,将变量、控制结构、过程和函数等结构化程序设计的要素引入了SQL语言中,这样就能够编制比较复杂的SQL程序了,利用PL/SQL语言编写的程序也称为...

    PLSQL基础word

    PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用PL/SQL,本文将讲述PL/SQL基础语法,结构和组件、以及如何设计并执行一个PL...

    PLSQL基础教程

    第三章 PL/SQL流程控制语句 13 §3.1 条件语句 13 §3.2 CASE 表达式 13 §3.3 循环 13 §3.3 标号和GOTO 13 §3.4 NULL 语句 13 第四章 游标的使用 13 §4.1 游标概念 13 §4.1.1 处理显式游标 13 §4.1.2...

    精通Oracle.10g.PLSQL编程

    编写控制结构 7.1 条件分支语句 7.2 CASE语句 7.3 循环语句 7.4 顺序控制语句 7.5 习题 第8章 使用复合数据类型 8.1 PL/SQL记录 8.1.1 定义PL/SQL记录 8.1.2 使用PL/SQL...

    Oracle 10g 学习笔记

    │ Oracle程序员开发指南.pdf │ └─北大青鸟oracle9i ├─1 │ -ORACLE公司传奇.doc │ 1作业.txt │ Oracle控制台使用.doc │ TP1V1.0.ppt │ 其它补充.txt │ 登录SQLPLUS.txt │ ├─2 │ 2上机...

    PL/SQL Developer8.04官网程序_keygen_汉化

    控制结构 PL/SQL程序段中有三种程序结构:条件结构、循环结构和顺序结构。  1) 条件结构 与其它语言完全类似,语法结构如下: if condition thenstatement1elsestatement2end if ;  2) 循环结构 这一结构与其他...

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(二)

     第13章 编写控制结构  第14章 使用复合数据类型  第15章 使用游标  第16章 异常处理 . 第17章 本地动态sql  第18章 pl/sql过程  第19章 pl/sql函数  第20章 pl/sql包  第21章 触发器  第22章 使用对象...

    软件工程师Java班课程

    软件工程和软件过程模型、版本控制 掌握业界软件开发规范和方式 J2EE商务应用系统项目开发 4~5人一个项目组,项目大小为(25人*工作日左右) 掌握大型商务系统开发的运作方式和开发方法 .net程序设计 掌握.net程序...

    Oracle PL SQL程序设计 上 第五版(代码示例)

    能够帮助你充分利用pl/sql来解决数据库开发中遇到的各种问题,引导你掌握各种构建应用的技巧和技术,以便使你编写出高效、可维护的代码。《oracle pl/sql程序设计(第5版)》不但介绍了大量的oracle 11g的pl/sql新性能...

    Oracle 11g SQL和PL SQL从入门到精通part2 pdf格式电子书 下载(二)

     第13章 编写控制结构  第14章 使用复合数据类型  第15章 使用游标  第16章 异常处理 . 第17章 本地动态sql  第18章 pl/sql过程  第19章 pl/sql函数  第20章 pl/sql包  第21章 触发器  第22章 使用对象...

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(一)

     第13章 编写控制结构  第14章 使用复合数据类型  第15章 使用游标  第16章 异常处理 . 第17章 本地动态sql  第18章 pl/sql过程  第19章 pl/sql函数  第20章 pl/sql包  第21章 触发器  第22章 使用对象...

    Oracle 11g SQL和PL SQL从入门到精通.part1

     第13章 编写控制结构  第14章 使用复合数据类型  第15章 使用游标  第16章 异常处理 . 第17章 本地动态sql  第18章 pl/sql过程  第19章 pl/sql函数  第20章 pl/sql包  第21章 触发器  第22章 使用对象...

    PL/SQL 基础.doc

    2) 扩展控制结构; 3) 扩展过程与函数; 4) 扩展对象类型与方法 ---- 第二章 PL/SQL程序结构 ---- 1. PL/SQL块 1) 申明部分, DECLARE (如果语句不需要声明任何变量,可以不写); 2) 执行部分, BEGIN &lt;-------...

    Oracle PL/SQL语言入门基础

    PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用PL/SQL,本文将讲述PL/SQL基础语法,结构和组件、以及如何设计并执行一个...

    Oracle9i的init.ora参数中文说明

    对于二进制排序, ORDER BY 查询的比较顺序是以数值为基础的。对于语言排序, 则需要进行全表扫描, 以便将数据按照所定义的语言排序进行整理。 值范围: BINARY 或有效的语言定义名。 默认值: 从 NLS_LANGUAGE 中获得...

Global site tag (gtag.js) - Google Analytics