oracle存储过程的创建与调用(实验8.3)

2024-03-28 12:32

本文主要是介绍oracle存储过程的创建与调用(实验8.3),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 alter user scott identified by a12345 account unlock;grant connect ,resource to scott;

1.创建存储过程,根据职工编号删除Scott.emp表中的相关记录。

(1)以Scott用户连接数据库,然后为system用户授予delete权限。conn scott/a12345;grant delete on emp to system;(2)以system 用户连接数据库,创建存储过程。
connect system/a12345;create or replace procedure delete_emp(id scott.emp.empno%type)is begin delete from scott.emp where empno=id;exception when others then dbms_output.put_line('errors');end;
/
(3)system 用户调用delete_emp存储过程。execute delete_emp(7369);
(4)scott 用户调用delete_emp存储过程。
grant execute on delete_emp to scott;
connect scott/a12345;
execute system.delete_emp(7369);
2.创建存储过程,根据职工编号修改scott.emp表中该职工的其他信息。

(1)   创建新用户,并授予权限。
connect system/a12345;create user u1 identified by abcdef;grant create session,create procedure to u1;grant select,update on scott.emp to u1;
(2)   以新用户连接数据库,创建存储过程。
connect u1/abcdef; CREATE OR REPLACE PROCEDURE update_emp(no IN scott.emp.empno%TYPE,--引用emp表中的某字段的数据类型,必须对该表具有select权限name IN scott.emp.ename%TYPE DEFAULT NULL,job1 IN scott.emp.job%TYPE DEFAULT NULL,mgr1 IN scott.emp.mgr%TYPE DEFAULT NULL,hiredate1 scott.emp.hiredate%TYPE DEFAULT NULL,salary scott.emp.sal%TYPE DEFAULT NULL,comm1 scott.emp.comm%TYPE DEFAULT NULL,deptno1 scott.emp.deptno%TYPE DEFAULT NULL)ISBEGINif name is not null thenupdate scott.emp set ename=name where empno=no;end if;if job1 is not null thenupdate scott.emp set job=job1 where empno=no;end if;if mgr1 is not null thenupdate scott.emp set mgr=mgr1 where empno=no;end if;if hiredate1 is not null thenupdate scott.emp set hiredate=hiredate1 where empno=no;end if;if salary is not null thenupdate scott.emp set sal=salary where empno=no;end if;if comm1 is not null thenupdate scott.emp set comm=comm1 where empno=no;end if;if deptno1 is not null thenupdate scott.emp set deptno=deptno1 where empno=no;end if;EXCEPTIONWHEN others THENrollback;END;/
(3)   u1调用update_emp 过程。exec update_emp(7369,salary=>2000);
3.创建存储过程,根据指定的职工编号查询该职工的详细信息。

(1)创建存储过程。
connect scott/a12345;create or replace procedure select_emp(no in scott.emp.empno%type,emp_information out varchar2)
is
r scott.emp%ROWTYPE;
begin select * into r from scott.emp where empno=no;emp_information:=emp_information||r.ename||'  '||r.job||'  '||r.sal||'   '||r.mgr||
'   '||r.hiredate||'   '||r.comm||'   '||r.deptno;
exceptionwhen no_data_found thenemp_information:='No person!';when others then emp_information:='Error!';
End;
/ (2)调用存储过程
set serveroutput ondeclare info varchar2(50);begin select_emp(7369,info);dbms_output.put_line(info);end;/
4.创建函数,根据给定的部门编号计算该部门所有职工的平均工资。
(1)创建函数。
create or replace function avg_sal(no scott.emp.deptno%type)return numberisavgsal number(7,2);beginselect avg(sal) into  avgsal from scott.emp where deptno=no;if  avgsal is not null then --因为上面的语句不触发异常,因此用if语句判断是否查询成功return  avgsal;elseavgsal:=-1;return  avgsal;end if;end   avg_sal;/
(2)调用函数。
begin dbms_output.put_line(avg_sal(&deptno));end;




这篇关于oracle存储过程的创建与调用(实验8.3)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/855620

相关文章

oracle 11g导入\导出(expdp impdp)之导入过程

《oracle11g导入导出(expdpimpdp)之导入过程》导出需使用SEC.DMP格式,无分号;建立expdir目录(E:/exp)并确保存在;导入在cmd下执行,需sys用户权限;若需修... 目录准备文件导入(impdp)1、建立directory2、导入语句 3、更改密码总结上一个环节,我们讲了

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

Java Kafka消费者实现过程

《JavaKafka消费者实现过程》Kafka消费者通过KafkaConsumer类实现,核心机制包括偏移量管理、消费者组协调、批量拉取消息及多线程处理,手动提交offset确保数据可靠性,自动提交... 目录基础KafkaConsumer类分析关键代码与核心算法2.1 订阅与分区分配2.2 拉取消息2.3

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

Java调用Python脚本实现HelloWorld的示例详解

《Java调用Python脚本实现HelloWorld的示例详解》作为程序员,我们经常会遇到需要在Java项目中调用Python脚本的场景,下面我们来看看如何从基础到进阶,一步步实现Java与Pyth... 目录一、环境准备二、基础调用:使用 Runtime.exec()2.1 实现步骤2.2 代码解析三、

MySQ中出现幻读问题的解决过程

《MySQ中出现幻读问题的解决过程》文章解析MySQLInnoDB通过MVCC与间隙锁机制在可重复读隔离级别下解决幻读,确保事务一致性,同时指出性能影响及乐观锁等替代方案,帮助开发者优化数据库应用... 目录一、幻读的准确定义与核心特征幻读 vs 不可重复读二、mysql隔离级别深度解析各隔离级别的实现差异