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

相关文章

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

PyInstaller打包selenium-wire过程中常见问题和解决指南

《PyInstaller打包selenium-wire过程中常见问题和解决指南》常用的打包工具PyInstaller能将Python项目打包成单个可执行文件,但也会因为兼容性问题和路径管理而出现各种运... 目录前言1. 背景2. 可能遇到的问题概述3. PyInstaller 打包步骤及参数配置4. 依赖

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

将Mybatis升级为Mybatis-Plus的详细过程

《将Mybatis升级为Mybatis-Plus的详细过程》本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率,通过本文,开发者可实现... 目录说明流程增加依赖修改配置文件注释掉MyBATisConfig里面的Bean代码生成使用IDEA生

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为