在网上查了好多资料,发现关于mysql的异常处理资料都是一些错误号列表,对于平时运行中,我们可能更多的希望能够记录准确的错误消息到日志中
下面是示例代码,在发生异常的时候会将异常信息存入日志表中,并继续运行后面的语句.如果您有更好的建议,望不吝赐教.
存储过程异常处理示例
代码如下:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`driveradmin`@`%` PROCEDURE `Merge_BrandProductKey`()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
begin
insert into t_runninglog values(default,default,'exception in MergeBrandProductKey',concat(@@error_count,' errors'));
commit;
end;
DECLARE CONTINUE HANDLER FOR SQLWARNING
begin
insert into t_runninglog values(default,default,'warnings in MergeBrandProductKey',concat(@@warning_count,' warnings'));
commit;
end;
insert into t_runninglog values(default,default,'start in MergeBrandProductKey','');
commit;
-- 任务执行主体 开始
-- /*
-- normal
update brandproductkey as bpk,
(select bp.brandproductid, bp.brandproductenname, bp.brandid
from brandproduct as bp
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid
) as bpp
set bpk.brandproductid=bpp.brandproductid
where bpk.brandproductid = 0
-- and bpk.computertype = 2 -- 0
and bpk.brandid = bpp.brandid
and upper(bpk.brandproductkeyname) = upper(replace(bpp.brandproductenname,' ',''));
commit;
insert into t_runninglog values(default,default,'rule normal in MergeBrandProductKey','');
commit;
-- sony rule 1
-- VPCEA37EC --> (VPCEA37EC/B,VPCEA37EC/L,VPCEA37EC/P,VPCEA37EC/W)
update brandproductkey as bpk,
(select bp.brandproductid, bp.brandproductenname, bp.brandid
from brandproduct as bp
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=60
) as bpp
set bpk.brandproductid=bpp.brandproductid
where bpk.brandproductid = 0
-- and bpk.computertype = 2 -- 0
and bpk.brandid = bpp.brandid
and bpp.brandproductenname like concat(bpk.brandproductkeyname,'/%');
commit;
insert into t_runninglog values(default,default,'rule sony 1 in MergeBrandProductKey','');
commit;
-- sony rule 2
-- VGN-TZ37N_X --> VGN-TZ37N/X
update brandproductkey as bpk,
(select bp.brandproductid, bp.brandproductenname, bp.brandid
from brandproduct as bp
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=60
) as bpp
set bpk.brandproductid=bpp.brandproductid
where bpk.brandproductid = 0
-- and bpk.computertype = 2 -- 0
and bpk.brandid = bpp.brandid
and upper(bpk.brandproductkeyname) = upper(replace(bpp.brandproductenname,'/','_'));
commit;
insert into t_runninglog values(default,default,'rule sony 2 in MergeBrandProductKey','');
commit;
-- lenovo rule 1
-- ZHAOYANG E45 --> 昭阳E45
update brandproductkey as bpk,
(select bp.brandproductid, bp.brandproductenname, bp.brandid,bpr.driverid
from brandproduct as bp
inner join (select brandid,brandproductid,max(driverinfoid) as driverid from brandproductdriverrelation group by brandid,brandproductid) as bpr
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=37
) as bpp
set bpk.brandproductid=bpp.brandproductid
where bpk.brandproductid = 0
-- and bpk.computertype = 2 -- 0
and bpk.brandid = bpp.brandid
and bpk.brandproductkeyname ''
and instr(bpp.brandproductenname,SUBSTRING_INDEX(bpk.brandproductkeyname,' ',-1))>0
and bpp.brandproductenname regexp concat('^[^\x00-\xff]+', SUBSTRING_INDEX(bpk.brandproductkeyname,' ',-1),'$');
commit;
insert into t_runninglog values(default,default,'rule lenovo 1 in MergeBrandProductKey','');
commit;
-- HP rule 1
-- HP Compaq 6535s --> HP Compaq 6535s 笔记本电脑
update brandproductkey as bpk,
(select bp.brandproductid, bp.brandproductenname, bp.brandid
from brandproduct as bp
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=36
) as bpp
set bpk.brandproductid = bpp.brandproductid
where bpk.brandproductid = 0
-- and bpk.computertype = 2 -- 0
and bpk.brandid = bpp.brandid
and bpk.brandproductkeyname ''
and bpp.brandproductenname = concat(bpk.brandproductkeyname,' 笔记本电脑');
insert into t_runninglog values(default,default,'rule hp 1 in MergeBrandProductKey','');
commit;
-- HP rule 2
-- HP Compaq 6535s --> HP Compaq 6535s Notebook PC
update brandproductkey as bpk,
(select bp.brandproductid, bp.brandproductenname, bp.brandid
from brandproduct as bp
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=36
) as bpp
set bpk.brandproductid = bpp.brandproductid
where bpk.brandproductid = 0
-- and bpk.computertype = 2 -- 0
and bpk.brandid = bpp.brandid
and bpk.brandproductkeyname ''
and upper(bpp.brandproductenname) = upper(concat(bpk.brandproductkeyname,' Notebook PC'));
insert into t_runninglog values(default,default,'rule hp 2 in MergeBrandProductKey','');
commit;
-- */
-- 任务执行主体 结束
insert into t_runninglog values(default,default,'finish in MergeBrandProductKey','');
commit;
END
有关HANDLER的语法结构如下:
代码如下:
DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement
handler_type: CONTINUE | EXIT
condition_value: SQLSTATE [VALUE] sqlstate_value | condition_name | SQLWARNING | NOT FOUND | SQLEXCEPTION | mysql_error_code
Handlers类型:
, EXIT: 发生错误时退出当前代码块(可能是子代码块或者main代码块)
, CONTINUE: 发送错误时继续执行后续代码
condition_value:
condition_value支持标准的SQLSTATE定义;
SQLWARNING是对所有以01开头的SQLSTATE代码的速记
NOT FOUND是对所有以02开头的SQLSTATE代码的速记
SQLEXCEPTION是对所有没有被SQLWARNING或NOT FOUND捕获的SQLSTATE代码的速记
除了SQLSTATE值,MySQL错误代码也被支持
但是对于mysql而言,优先级如下:
MySQL Error code > SQLSTATE code > 命名条件

标题:Oracle存储过程实现批量更新的步骤与注意事项在Oracle数据库中,存储过程是一组为了提高数据库性能、重用代码、增强安全性的SQL语句集合,通过存储过程可以实现批量更新数据的操作。本文将介绍如何使用Oracle存储过程实现批量更新,并提供具体的代码示例。步骤一:创建存储过程首先,我们需要创建一个存储过程,用来实现批量更新的操作。以下是创建存储过程的

Oracle数据库中存储过程是一种特定类型的存储过程,用于在数据库中执行一系列的SQL语句和数据操作。在实际的数据库开发工作中,有时候我们需要判断某个表是否存在于数据库中,这样可以在存储过程中做一些判断和逻辑处理。下面我们将介绍如何在Oracle数据库中实现判断表是否存在的方法,并提供具体的代码示例。首先,我们可以利用系统表user_tables或all_t

MySQL删除存储过程的方法有使用DROP PROCEDURE语句、使用MySQL Workbench和使用命令行工具等。详细介绍:1、使用DROP PROCEDURE语句,其步骤为先打开MySQL客户端或使用任何支持MySQL的工具,再连接到您的MySQL数据库,最后执行以下SQL语句来删除存储过程;2、使用MySQL Workbench删除存储过程等等。

Golang存储过程的实现原理与应用存储过程是一种在关系数据库中存储并能被应用程序调用的预编译程序,可以有效地减少网络传输数据的开销,提高数据库的执行效率。虽然Golang并不直接支持存储过程,但是可以通过使用SQL语句来模拟实现存储过程的功能。本文将介绍Golang中实现存储过程的原理和应用,并提供具体的代码示例。一、Golang存储过程的实现原理在Gol

Oracle存储过程批量更新的性能优化策略在Oracle数据库中,存储过程是一种用来处理数据逻辑或执行特定任务的数据库对象,可以提供一定的性能优化策略,特别是在批量更新数据时。批量更新数据通常会涉及大量的行级操作,为了提高性能和效率,我们可以采取一些策略和技巧来优化存储过程的性能。下面将介绍一些Oracle存储过程批量更新的性能优化策略,并提供具体的代码示例

标题:Oracle存储过程与函数详细对比及优势分析在Oracle数据库中,存储过程和函数是两种重要的数据库对象,它们都可以用来封装一系列的SQL语句和逻辑,提高数据操作的效率和复用性。本文将详细对比Oracle存储过程和函数的特点,以及它们各自的优势所在,并提供具体的代码示例。存储过程存储过程是一组预先编写好并存储在数据库中的SQL语句和PL/SQL代码逻辑

Golang是一门强大的编程语言,它能够轻松地实现存储过程。在本文中,我们将介绍如何使用Golang编写高效的存储过程,以及在项目中使用它们的好处。

如何在MySQL中使用C#编写自定义存储过程和函数引言:MySQL是一个广泛使用的开源数据库管理系统,而C#是一种常用的面向对象的编程语言。在开发过程中,我们经常需要使用数据库存储过程和函数来提高代码的复用性和性能。本文将介绍如何在MySQL数据库中使用C#编写自定义存储过程和函数,并提供具体的代码示例。一、存储过程存储过程是一组为执行特定任务的SQL语句集


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
