oracle 触发器 merge,[OT]函数|过程|触发器|插入(insert)|修改(Merge)

CREATE TABLE errlog

(Errcode NUMBER,Errtext CHAR(40));

--1,创建函数:

CREATE OR REPLACE

FUNCTION get_salary (p_deptno NUMBER ) RETURN NUMBER AS v_sal NUMBER;

BEGIN

IF p_deptno IS NULL THEN

RAISE_APPLICATION_ERROR(-20991,'Department number is null');

ELSIF p_deptno < 0 THEN

RAISE_APPLICATION_ERROR(-20992,'Invalide department number');

ELSE

SELECT SUM(sal) INTO v_sal FROM EMP WHERE deptno=p_deptno;

RETURN v_sal;

END IF;

END;

--2,打开包得输出output

SET SERVEROUTPUT ON

--3,调用函数

DECLARE

v_salary       NUMBER(7,2);

v_sqlcode      NUMBER;

v_sqlerr       VARCHAR2(512);

Null_deptno    EXCEPTION;

Invalid_deptno EXCEPTION;

PRAGMA EXCEPTION_INIT(null_deptno,-20991);

PRAGMA EXCEPTION_INIT(invalid_deptno,-20992);

BEGIN

v_salary :=get_salary(10);

DBMS_OUTPUT.PUT_LINE('10 department salary is :'||TO_CHAR(v_salary));

BEGIN

v_salary :=get_salary(-10);

EXCEPTION

WHEN invalid_deptno THEN

v_sqlcode := SQLCODE;

v_sqlerr  := SQLERRM;

INSERT INTO errlog

(errcode,errtext) VALUES

(v_sqlcode,v_sqlerr);

COMMIT;

END inner1;

v_salary :=get_salary(20);

DBMS_OUTPUT.PUT_LINE('20 department salary is:' ||TO_CHAR(v_salary));

BEGIN

v_salary :=get_salary(NULL);

END inner2;

v_salary :=get_salary(30);

DBMS_OUTPUT.PUT_LINE('30 department salary is :'||TO_CHAR(v_salary));

EXCEPTION

WHEN null_deptno THEN

v_sqlcode :=SQLCODE;

v_sqlerr  :=SQLERRM;

INSERT INTO errlog

(errcode,errtext

) VALUES

(v_sqlcode,v_sqlerr

);

COMMIT;

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('Other errores!');

END outer;

--4,创建存储过程

create or replace PROCEDURE Get_emp_rec(Emp_number IN EMP.empno%TYPE,

Emp_ret OUT Emp%ROWTYPE) IS

BEGIN

select * into Emp_ret

FROM emp Where Empno=Emp_number;

end;

--5,调用存储过程

declare

emp_number int :=7900;

emp_ret emp%rowtype;

begin

get_emp_rec(emp_number,emp_ret);

dbms_output.put_line(emp_ret.ename);

end;

--6,触发器

create table emp_test as select * from emp;

create table emp_test1 as select * from emp;

CREATE OR REPLACE TRIGGER del_emp

BEFORE DELETE OR UPDATE ON scott.emp_test1 FOR   EACH ROW

BEGIN

INSERT INTO emp_test(deptno , empno, ename , job ,mgr , sal , comm , hiredate )

VALUES( :old.deptno, :old.empno, :old.ename , :old.job,

:old.mgr, :old.sal, :old.comm, :old.hiredate );

END;

--7,测试触发器

select * from emp_test1;

delete from emp_test1 where deptno=20;

commit;

select count(*) from emp_test;

--8,视图触发器

create or replace view emp_view as

select deptno,count(*) total_employeer,sum(sal) total_salary

from emp_test group by deptno;

select * from emp_view;

CREATE OR REPLACE TRIGGER emp_view_delete

INSTEAD OF DELETE ON emp_view FOR EACH ROW

BEGIN

DELETE FROM emp_test WHERE deptno= :old.deptno;

END emp_view_delete;

--9,测试视图触发器

delete from emp_view where deptno=10;

commit;

select * from emp_test;

--10,登录触发器

create table logtable(username varchar2(50),logindate date);

create or replace trigger login_his

after logon on database

begin

insert into logtable

values(user,sysdate);

end;

--11,包定义

CREATE OR REPLACE package DXJF_MGR

AS

time1 number(10) := 0;

type cursorref_t is ref cursor;

type r_rate_t is record

(

no NUMBER(10),

rate NUMBER(10),

starttime date,

endtime date,

timespanid NUMBER(10)

);

type array_rate_t IS VARRAY(100) OF r_rate_t;

function f_is2day(dt date,duration number) return number;

procedure p_test(p_result in number :=0);

END DXJF_MGR;

--12,程序类型 :函数 判断通话时间否跨越2天

CREATE OR REPLACE FUNCTION f_is2day(dt date,duration number)

return number

is

v_date1 varchar2(10);

v_date2 varchar2(10);

v_date3 date;

v_date4 date;

i_day number(10);

begin

select (dt+duration/24/60/60) into v_date3 from dual;

select trunc(dt+1,'J') into v_date4 from dual;

if v_date3<=v_date4 then

return 0;

else

return 1;

end if;

end;

--13,Unconditional INSERT ALL

create table sal_history as select empno,hiredate,sal from emp where 1=0;

create table mgr_history as select empno,mgr,sal from emp where 1=0;

insert all

into sal_history values(empno,hiredate,sal)

into mgr_history values(empno,mgr,sal)

select empno,hiredate,mgr,sal from emp

where empno >800

select * from sal_history;

select * from mgr_history;

--14,Conditional INSERT ALL

create table sal_history as select empno,hiredate,sal from emp where 1=0;

create table mgr_history as select empno,mgr,sal from emp where 1=0;

INSERT ALL

WHEN SAL > 3000 THEN

INTO sal_history VALUES(empno,hiredate,sal)

WHEN MGR > 4000 THEN

INTO mgr_history VALUES(empno,mgr,sal)

select empno,hiredate,mgr,sal from emp

WHERE empno > 1000;

truncate table sal_history;

truncate table mgr_history;

select * from sal_history;

select * from mgr_history;

select * from emp;

--15,Conditional INSERT FIRST

create table special_sal as select deptno,sal from emp where 1=0;

create table hiredate_history_00 as select deptno,hiredate from emp where 1=0;

create table hiredate_history_99 as select deptno,hiredate from emp where 1=0;

create table hiredate_history as select deptno,hiredate from emp where 1=0;

insert first

when sal > 10000 then

into special_sal values(DEPTID,SAL)

when HIREDATE like('%00%')THEN

INTO hiredate_history_00 values(DEPTID,HIREDATE)

when hiredate like('%99%')THEN

into hiredate_history_99 values(deptid,hiredate)

else

INTO hiredate_history values(DEPTID,HIREDATE)

select deptno deptid,sum(sal) sal,max(hiredate)HIREDATE

from emp

group by deptno;

select * from special_sal;

select * from hiredate_history_00

select * from hiredate_history_99

select * from hiredate_history

--16,Pivoting insert

create table sales_source_data (employee_id number(6),week_id number(2),

sales_mon number(8,2),sales_tue number(8,2),sales_wed number(8,2),

sales_thur number(8,2),sales_fri number(8,2));

insert into sales_source_data values (176,6,2000,3000,4000,5000,6000);

commit;

select * from sales_source_data;

create table sales_info(employee_id number(6),week number(2),sales number(8,2));

insert all

INTO sales_info VALUES (employee_id,week_id,sales_MON)

INTO sales_info VALUES (employee_id,week_id,sales_TUE)

INTO sales_info VALUES (employee_id,week_id,sales_WED)

INTO sales_info VALUES (employee_id,week_id,sales_THUR)

INTO sales_info VALUES (employee_id,week_id,sales_FRI)

select EMPLOYEE_ID,week_id,sales_MON,sales_TUE,

sales_WED,sales_THUR,sales_FRIfrom sales_source_data;

select * from sales_info;

create table tmp2 as select * from emp;

delete from emp2 where rownum<10;

commit;

--17,MERGE Statement

create table emp2 as select * from emp;

MERGE INTO emp2 e2

USING emp e

ON (e2.empno = e.empno)

WHEN MATCHED THEN

UPDATE SET

e2.ename = e.ename,

e2.job = e.job,

e2.mgr = e.mgr,

e2.sal = e.sal,

e2.comm = e.comm,

e2.deptno = e.deptno

WHEN NOT MATCHED THEN

INSERT VALUES(e.empno, e.ename, e.job,e.mgr, e.hiredate, e.sal, e.comm,e.deptno);

select count(*) from emp2;

--18,MERGE Statement

create table orders_master (order_id int,order_total number(10));

create table monthly_orders (order_id int,order_total number(10));

insert into orders_master values (1,1000);

insert into orders_master values (2,2000);

insert into orders_master values (3,3000);

insert into orders_master values (4,null);

insert into monthly_orders values (2,2500);

insert into monthly_orders values (3,null);

merge into orders_master o

using monthly_orders m on (o.order_id=m.order_id)

when matched then

update set o.order_total=m.order_total

delete where (m.order_total is null)

when not matched then

insert values (m.order_id,m.order_total);

select * from orders_master;

select * from monthly_orders;

select * from orders_master;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/267715.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

苏州飘“彩云” 五年规模破百亿元

7月12日在苏州举行的国际云计算与移动互联网应用研讨会的重头戏之一&#xff0c;是苏州科技城 “彩云计划”的发布。由硅谷云计算卓越中心&#xff08;CoE&#xff09;与苏州科技城联合设立的苏州科技城云计算卓越中心同时成立。苏州科技城云计算卓越中心的建立是“彩云计划”落…

php 生成导出excel,PHP导出生成EXCEL文件

PHP导出生成EXCEL文件// 解决中文乱码所做的编码转换function xecho($content){echo mb_convert_encoding($content,"gbk","auto");}// 必须要的基本headerheader("Content-Type: application/vnd.ms-excel; charsetutf-8");header("Conten…

linux 内核 内存管理 slub算法 (一) 原理

http://blog.csdn.net/lukuen/article/details/6935068转载于:https://www.cnblogs.com/zengkefu/p/8320429.html

用户配置文件

问&#xff1a;什么是用户配置文件? 答&#xff1a;用户配置文件就是在用户登录时定义系统加载所需环境的设置和文件的集合。它包括所有用户专用的配置设置&#xff0c;如程序项目、屏幕颜色、网络连接、打印机连接、鼠标设置及窗口的大小和位置。 当你第一次登录到一台基于…

oracle00109,ORA-01034: 、ORA-01078: 和 LRM-00109: 的解决方法,ora-01034ora-01078

ORA-01034: 、ORA-01078: 和 LRM-00109: 的解决方法&#xff0c;ora-01034ora-01078环境&#xff1a;Linux 5.4 Oracle 11.2.0.3在Linux上连接Oracle时遇到报错&#xff1a;SQL> show parameter sgaORA-01034: ORACLE not availableProcess ID: 0Session ID: 0 Serial numbe…

listView 多布局

最近在开发项目中遇到了实现类似淘宝首页的需求&#xff0c;使用listView可以解决&#xff0c;在此记录一下。 实现步骤&#xff1a; 重写 getViewTypeCount() – 返回你有多少个不同的布局重写 getItemViewType(int) – 由position返回view type id根据view item的类型&#x…

数据查询

command命令的方法&#xff1a;ExecuteNonQuery(受影响行数),ExecuteReader(返回的是Datatable),ExecuteScalar(第一行第列) SqlDataAdapter是和DataSet配和工作的&#xff0c;不能分开。 using(SqlConnection connnew SqlConnection(connectionString)) { conn.Open(); SqlCom…

Php面试题之背人过桥,梦见背人过桥好不好?

吉凶指数&#xff1a;99(由佛滔居士根据数理文化得出&#xff0c;仅供参考)老年人梦见桥&#xff0c;意味着将不久于人世。谈判人员梦见桥&#xff0c;意味着与对手实现双赢。梦见走过土桥的梦&#xff0c;也表示谈生意、婚姻等拖延不决。梦见过桥&#xff0c;表示有能力解决遇…

第00章—IDEA

spring boot 系列学习记录&#xff1a;http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址&#xff1a;https://gitee.com/jinxiaohang/springboot 来自网络搜索整理 IntelliJ IDEA安装 IntelliJ IDEA破解 IntelliJ IDEA 注册码 IntelliJ IDEA安装主题 IntelliJ …

软件测试5未来大发展路线,测试工程师发展偏向

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 1、软件测试的五大…

SQL里的SWITCH分支语句

declareiintdeclarejvarchar(10)seti1selectjcaseiwhen1then11when2then22when3then33elseotherendprintj

linux 文件夹换所属用户,linux普通用户su root切换提示没有文件或目录的解决方法...

1. 首先进入单用户模式&#xff1a;1). ubuntu :上述情况可以在grub界面选择第二项修复&#xff0c;但没有grub可以参考&#xff1a;1、重启ubuntu&#xff0c;随即长按shirft进入grub菜单&#xff1b;2、选择recovery mode&#xff0c;按"e"键进入编辑页面&#xff…

排序算法[转]

笔者最近学习算法&#xff0c;学了很久也只弄懂了几个排序算法&#xff0c;在这里晒一下下&#xff0c;作为以后参考之用。 一、为什么要研究排序问题 许多计算机科学家认为&#xff0c;排序算法是算法学习中最基本的问题&#xff0c;原因有以下几点&#xff1a; l 有时候应用…

linux 搜索 文件 内容,Linux 文件查找及文件内容查找

使用grep搜索文件内容——快捷、方便(1) 在当前目录下的所有文件的文件内容中查找哪个文件的内容中有findcontents(大小写不敏感&#xff0c;列出findcontents所在文件的所在行)——适合于当前目录下的文件及目录数目比较少&#xff0c;如果查找后列出内容过多&#xff0c;将会…

sonar做代码检测时如何忽略一些代码文件

1、管理员登录sonar 2、如图 一条规则配置一个&#xff0c;不要填写逗号或者分号分割的多个规则 转载于:https://www.cnblogs.com/shengulong/p/8324764.html

回车符和换行符

2019独角兽企业重金招聘Python工程师标准>>> 首先转一段关于回车和换行的历史。 回车和换行 关于“回车”&#xff08;carriage return&#xff09;和“换行”&#xff08;line feed&#xff09;这两个概念的来历和区别。 在计算机还没有出现之前&#xff0c;有一种…

linux新建好文件后怎么编译,使用autoconf生成Makefile并编译工程的步骤

前言在Linux下&#xff0c;编译和链接需要用Makefile&#xff0c;而写好一个Makefile可不容易&#xff0c;写出一个规范的Makefile更是麻烦。我们可以利用autoconf来生成一个Makefile&#xff0c;尤其在大型工程里&#xff0c;能省去很多麻烦。这里我会用一个工程来说明怎么做&…

前端跨域的那些事

这一节&#xff0c;我们来讲一讲&#xff0c;前端跨域的那些事&#xff0c;主要分成这样的几部分来讲解&#xff0c; 一、为什么要跨域&#xff1f; 二、常见的几种跨域与使用场景 2.1 JSONP跨域 2.2 iframe跨域 2.3 window.name 跨域 2.4 document.domain 跨域 2.5 cookie跨域…

VC++学习(15):多线程

1. 程序,进程,线程 A: 程序是计算机指令的集合,它以文件的形式存储在磁盘上,而进程通常被定义为一个正在运行的程序的实例,是一个程序在其自身的地址空间中的一次执行活动.一个程序可以对应多个进程. 进程是资源申请,高度和独立运行的单位,因此,它使用系统中的运行资源,而…

在linux中500g怎么分区,500G的硬盘,怎么分区比较合理?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼个人电脑怎么分&#xff1f;希望大家给我规范一下另外&#xff0c;我的/dev/sda2用的是xfs&#xff0c;/dev/sda9用的是reiser4draplaterDrapl ~ $ sudo fdisk -l密码&#xff1a;Disk /dev/sda: 160.0 GB, 160041885696 bytes255 …