时间:2021-07-01 10:21:17 帮助过:18人阅读
十三、存储过程和存储函数
1、掌握存储过程(相当于建立一个函数或者方法体,然后通过外部对其调用)
   指存储在数据库中供所有程序调用的子程序叫做存储过程或存储函数。
   相同点:
         完成特定功能的程序
   区别:
       是否用return语句返回值
    
    (1)创建和使用存储过程
       用create procedure命令建立存储过程和存储函数
       语法:
           create or replace procedure 过程名(参数列表) as PL/SQL 子程序体(说明部分);
       事例:
           (a)打印一个存储过程:打印HelloWorld(无参数)
              create or replace procedure sayhelloworld
	        as 
		  --说明部分
                begin
		   dbms_output.put_line(‘Hello  World !!!‘);
		end;
		/
              在编译器左边的过程里形成大写的SAYHELLOWORLD
	      对其调用:
	      方法一:
	            execute 简写为exec sayhelloworld();
	      方法二:
	            begin
		      sayhelloworld();
		      sayhelloworld();
		    end;
		    /
             (b)带参数的存储过程
	     事例:为指定的员工,涨100块钱的工资,并且打印涨前和涨后的薪水
	        思路:1、创建一个带参数的存储过程
		     2、给指定的员工涨100块钱的工资,并且打印涨前和涨后的薪水
	        create or replace procedure raisesalary(eno in number)
	        as 
		  --定义一个变量保存涨钱的薪水
		  psal emp.sal%type;
                begin
		   --得到员工涨前的薪水
		   select sal into  psal from emp where empno=eno;
		   --给员工涨100
		   update emp set sal=sal+100 where empno=eno;
		   dbms_output.put_line(‘涨前薪水 :‘||psal||‘涨后薪水 :‘||(psal+100));
		   --是否需要commit?
		   --注意:一般不在存储过程或者存储函数中,commit和rollback.原则不绝对
		end;
		/
		如何调用:
		     begin
		         raisesalary(7839);
			 raisesalary(7566);
			 commit;
		      end;
            如何调试
                注意:不推荐远程调试,推荐本地调试
	  过程-->右键-->编译以进行调试
         
	  设置断点
	      select sal into  psal from emp where empno=eno;
          点击瓢虫调试
	       更改值 ENO := 7839; 点击确定
          授权
	  系统认证登录:sqlplus / as sysdba
	  grant DEBUG CONNECT SESSION ,DEBUG ANY PROCEDURE to scott;
	  右键点击检测psal
	  调试
	  点击F8
2、掌握存储函数
       函数为一命名的存储程序,可带参数,并返回一计算值.
       函数和过程的结构类似,但必须有一个return子句,用于返回函数值.
   (1)创建存储函数的语法
      create or replace function 函数名(参数列表)
      return 函数值类型
      as 
      PL/SQL子程序体;
      事例:查询某个员工的年收入
           create or replace function queryempincome(eno in number)
	   return number
	   as
	      --定义变量保存员工的薪水和奖金
	      psal emp.sal%type;
	      pcomm emp.comm%type;
	      begin
                   --得到该员工的月薪和奖金
		   select sal,comm into psal,pcomm from emp where empno=eno;
		   --直接返回年收入
		   return psal*12+nvl(pcomm,0);
	      end;
	      /
	      set linesize 200
	      select * from emp;   
调用存储函数
     (2)in和out参数
       一般来讲,存储过程和存储函数的区别在于存储函数可以有一个返回值,而存储过程没有返回值.
       存储过程和存储函数都可以有out参数
       存储过程和存储函数都可以有多个out参数
       存储过程可以通过out参数来实现返回值
     什么时候用存储过程/存储函数?
     原则:
         -- 如果只有一个返回值,用存储函数,否则,就用存储过程.
     ----out参数,查询某个员工姓名,月薪和职位
         create or replace procedure queryempinform(eno in number, pename out varchar2,psal out number,pjob out varchar2)
	 as
	 begin
	      --得到该员工的姓名,月薪和职位
	      select ename,sal,empjob into pename,psal,pjob from emp where empno=eno;
	 end;
	 /
	 思考:(1)查询某个员工的所有信息---->out参数太多了
	 set linesize 80
	 desc emp
              (2)查询某个部门中所有员工的所有信息--->out中返回集合
       (3)在应用程序中访问存储过程和存储函数
          (a)访问存储过程
	     public class JdbcUtils {
	private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	private static String username = "scott";
	private static String password = "tiger";
	private static Connection conn;
	public static Connection getConnection() {
		conn = null;
		try {
			conn = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("oracle连接获取失败");
		}
		return conn;
	}
	// 关闭连接对象
	public static void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("oracle连接关闭失败");
			}
		}
	}
	public static void close(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("oracle连接关闭失败");
			}
		}
	}
	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("oracle连接关闭失败");
			}
		}
	}
}
public class TestProcedure {
	@Test
	public void testProcedure() {
		String sql = "{call queryempinform(?,?,?,?)}";
		Connection conn = null;
		CallableStatement call = null;
		try {
			// 得到一个连接
			conn = JdbcUtils.getConnection();
			// 通过连接创建出statement
			call = conn.prepareCall(sql);
			
			//对于in参数,赋值
			call.setInt(1, 7839);
			//对于out参数,申明
             call.registerOutParameter(2, OracleTypes.VARCHAR);
             call.registerOutParameter(3, OracleTypes.NUMBER);
             call.registerOutParameter(4, OracleTypes.VARCHAR);
             
             //执行调用
             call.execute();
             
             //取出结果
             String name =call.getString(2);
             double sal =call.getDouble(3);
             String job =call.getString(4);
             System.out.println(name+"\t"+sal+"\t"+job);
             
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.close(conn);
			JdbcUtils.close(call);
		}
	}
}
	  (b)访问存储函数
           public class TestFunction {
	@Test
	public void testFunction() {
		String sql = "{?=call queryempincome(?)}";
		Connection conn = null;
		CallableStatement call = null;
		try {
			// 得到一个连接
			conn = JdbcUtils.getConnection();
			// 通过连接创建出statement
			call = conn.prepareCall(sql);
			// 对于输出参数,申明
			call.registerOutParameter(1, OracleTypes.NUMBER);
			// 对于参数赋值
			call.setInt(2, 7839);
			// 执行调用
			call.execute();
// 取出结果
			double income = call.getDouble(1);
			
			//打印结果集
			System.out.println("年收入:" + income);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
			JdbcUtils.close(call);
		}
	}
}
(4)在out参数中使用光标
	 (a)申明包结构
	 (b)包头
	 (c)包体
          
	  事例:查询某个部门中所有员工的所有信息
	  包头:
	      create or replace package mypackage as
	           --type自定义类型empcursor
	           type empcursor is ref cursor;
		   procedure queryEmpList(dno in number,empList out empcursor);
	      end mypackage;
             *****包体需要实现包头中生命的所有方法*******
	     操作:
	         程序包----->右键--->新建程序包--->包名设置mypackage--->确定--->编译--->ctril+s执行
         
	 包体:
	     create or replace package body mypackage as
	       
		   procedure queryEmpList(dno in number,empList out empcursor) as
		   begin
		        open empList for select * from emp where deptno =dno;
		   end queryEmpList;
	      end mypackage;
	      操作:
	          选中mypackage-->右键-->创建主体--->编译--->ctril+s执行
        查看包:
	      desc mypackage  
	注意:需要带上包名访问
       (5)在应用中访问包中的存储过程
          public class TestMypackage {
	@Test
	public void testMypackage() {
		String sql = "{call MYPACKAGE.queryEmpList(?,?)}";
		Connection conn = null;
		CallableStatement call = null;
		ResultSet rs = null;
		try {
			// 得到一个连接
			conn = JdbcUtils.getConnection();
			// 通过连接创建出statement
			call = conn.prepareCall(sql);
			// 对于参数赋值
			call.setInt(1, 10);
			// 对于输出参数,申明
			call.registerOutParameter(2, OracleTypes.CURSOR);
			// 执行调用
			call.execute();
// 取出该部门的所有员工信息
			rs = ((OracleCallableStatement) call).getCursor(2);
			while (rs.next()) {
				// 该员工的员工号/薪水/职位
				int empno =rs.getInt("empno");
				String name =rs.getString("ename");
				double salary = rs.getDouble("sal");
				String job = rs.getString("empjob");
				System.out.println(empno +"\t"+name+"\t"+salary+"\t"+job);
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
			JdbcUtils.close(call);
			JdbcUtils.close(rs);
		}
	}
}
Oracle学习笔记(十二)
标签:检测 语句 变量 列表 系统 如何 类型 ring pre