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

相关文章

JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)

《JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)》:本文主要介绍如何在IntelliJIDEA2020.1中创建和部署一个JavaWeb项目,包括创建项目、配置Tomcat服务... 目录简介:一、创建项目二、tomcat部署1、将tomcat解压在一个自己找得到路径2、在idea中添加

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

Java利用Spire.Doc for Java实现在模板的基础上创建Word文档

《Java利用Spire.DocforJava实现在模板的基础上创建Word文档》在日常开发中,我们经常需要根据特定数据动态生成Word文档,本文将深入探讨如何利用强大的Java库Spire.Do... 目录1. Spire.Doc for Java 库介绍与安装特点与优势Maven 依赖配置2. 通过替换

JAVA SpringBoot集成Jasypt进行加密、解密的详细过程

《JAVASpringBoot集成Jasypt进行加密、解密的详细过程》文章详细介绍了如何在SpringBoot项目中集成Jasypt进行加密和解密,包括Jasypt简介、如何添加依赖、配置加密密钥... 目录Java (SpringBoot) 集成 Jasypt 进行加密、解密 - 详细教程一、Jasyp

Java通过ServerSocket与Socket实现通信过程

《Java通过ServerSocket与Socket实现通信过程》本文介绍了Java中的ServerSocket和Socket类,详细讲解了它们的构造方法和使用场景,并通过一个简单的通信示例展示了如何... 目录1 ServerSocket2 Socket3 服务器端4 客户端5 运行结果6 设置超时总结1

在C#中调用Windows防火墙界面的常见方式

《在C#中调用Windows防火墙界面的常见方式》在C#中调用Windows防火墙界面(基础设置或高级安全设置),可以使用进程启动(Process.Start)或Win32API来实现,所以本文给大家... 目录引言1. 直接启动防火墙界面(1) 打开基本防火墙设置(firewall.cpl)(2) 打开高

java创建xls文件放到指定文件夹中实现方式

《java创建xls文件放到指定文件夹中实现方式》本文介绍了如何在Java中使用ApachePOI库创建和操作Excel文件,重点是如何创建一个XLS文件并将其放置到指定文件夹中... 目录Java创建XLS文件并放到指定文件夹中步骤一:引入依赖步骤二:创建XLS文件总结Java创建XLS文件并放到指定文件

python调用dubbo接口的实现步骤

《python调用dubbo接口的实现步骤》本文主要介绍了python调用dubbo接口的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录 ​​其他实现方式与注意事项​​ ​​高级技巧与集成​​用 python 提供 Dubbo 接口

C# FTP调用的实现示例

《C#FTP调用的实现示例》本文介绍了.NET平台实现FTP/SFTP操作的多种方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1. 使用 .NET 自带 FtpWebRequest 实现 FTP 操作1.1 文件上传1.2

MongoDB搭建过程及单机版部署方法

《MongoDB搭建过程及单机版部署方法》MongoDB是一个灵活、高性能的NoSQL数据库,特别适合快速开发和大规模分布式系统,本文给大家介绍MongoDB搭建过程及单机版部署方法,感兴趣的朋友跟随... 目录前言1️⃣ 核心特点1、文档存储2、无模式(Schema-less)3、高性能4、水平扩展(Sh