<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sqlstate &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/sqlstate/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 20 Nov 2017 05:50:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>MySQL Error Handling</title>
		<link>https://www.dbrnd.com/2015/05/mysql-error-handling/</link>
					<comments>https://www.dbrnd.com/2015/05/mysql-error-handling/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 21 May 2015 15:27:08 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[continue]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exception stored procedure]]></category>
		<category><![CDATA[exit]]></category>
		<category><![CDATA[handler]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[sqlstate]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=159</guid>

					<description><![CDATA[In this post, I am sharing the full demonstration on how to manage error/exception handling in the Stored Procedure of MySQL. Whenever an exception is occurring in a stored procedure, it is very important to handle it by showing proper error messages. If you do not handle the exception, there would be a chance to [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fmysql-error-handling%2F&amp;linkname=MySQL%20Error%20Handling" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fmysql-error-handling%2F&amp;linkname=MySQL%20Error%20Handling" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fmysql-error-handling%2F&amp;linkname=MySQL%20Error%20Handling" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fmysql-error-handling%2F&amp;linkname=MySQL%20Error%20Handling" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fmysql-error-handling%2F&amp;linkname=MySQL%20Error%20Handling" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fmysql-error-handling%2F&amp;linkname=MySQL%20Error%20Handling" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fmysql-error-handling%2F&#038;title=MySQL%20Error%20Handling" data-a2a-url="https://www.dbrnd.com/2015/05/mysql-error-handling/" data-a2a-title="MySQL Error Handling"></a></p><p>In this post, I am sharing the full demonstration on how to manage error/exception handling in the Stored Procedure of MySQL. </p>
<p>Whenever an exception is occurring in a stored procedure, it is very important to handle it by showing proper error messages.</p>
<p>If you do not handle the exception, there would be a chance to fail application with the certain exception in a stored procedure.</p>
<blockquote><p><b>If you get an error in stored procedure, instead of an exit, you should continue without any error message. That means you can show any default or custom error code or message to the application/user.</b></p></blockquote>
<p>MySQL provides Handler to handle the exception in the stored procedure.</p>
<p><strong>Below is a full demonstration of a handler with examples:</strong></p><pre class="crayon-plain-tag">/*Create Employee database for demo */
CREATE DATABASE Employee;
/*Create sample EmployeeDetails table.*/
CREATE TABLE Employee.tbl_EmployeeDetails 
(
     EmpID INTEGER 
    ,EmpName VARCHAR(50)
    ,EmailAddress VARCHAR(50)    
    ,CONSTRAINT pk_tbl_EmployeeDetails_EmpID PRIMARY KEY (EmpID)
)ENGINE = InnoDB;</pre><p>
<b>How to declare handler in store procedure:</b></p>
<p><b>Syntax of Handler:</b> <b><br />
</b></p><pre class="crayon-plain-tag">DECLARE handler_action HANDLER FOR condition_value ... statement</pre><p>
<b>Three type of Handler_Action: </b></p>
<ul>
<li>CONTINUE</li>
<li>EXIT</li>
<li>UNDO</li>
</ul>
<div>
<p><b>Type of Condition Value:</b></p>
</div>
<div>
<ul>
<li>mysql_error_code</li>
<li>sqlstate_value</li>
<li>SQLWarning</li>
<li>SQLException</li>
<li>NotFound</li>
</ul>
<div>
<p><b>How to write handler in stored procedure?</b></p>
<p><b>E.g.</b></p><pre class="crayon-plain-tag">DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SELECT 'Error occured';
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET IsError=1;    
DECLARE EXIT HANDLER FOR SQLEXCEPTION SET IsError=1;
DECLARE EXIT HANDLER FOR SQLSTATE '23000' SET IsError = 1;</pre><p>
</div>
</div>
<p>The Above are four different handler examples. Now, I am going to insert a duplicate value into EmpID column.</p>
<pre class="crayon-plain-tag">DELIMITER //
CREATE PROCEDURE Employee.usp_InsertEmployeeDetails
(
     InputEmpID INTEGER
    ,InputEmpName VARCHAR(50)
    ,InputEmailAddress VARCHAR(50)    
)
/****************************************************************
Authors Name : Anvesh Patel
Created Date : 2015-05-20
Description : This is demo stored procedure to 
              insert record into table with proper 
              error handling.Basically for www.dbrnd.com readers. 
*****************************************************************/
BEGIN 
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SELECT 'Error occured';
 INSERT INTO Employee.tbl_EmployeeDetails
 (
  EmpID
  ,EmpName 
  ,EmailAddress  
 )
 VALUES
 (
  InputEmpID
  ,InputEmpName 
  ,InputEmailAddress   
 );    
 SELECT *FROM Employee.tbl_EmployeeDetails;
END 
// DELIMITER ;</pre>
<p>In the above SP, I defined a CONTINUE handler with my custom exception message.</p>
<p>Now, call the above SP two times with same EmpID.</p>
<p>The first time, it will execute successfully, but the second time it will throw a custom error message.</p>
<p>As we defined CONTINUE handler, so it will just show an error message and CONTINUE to next part of the SELECT statement.</p>
<p>Let&#8217;s See,</p>
<pre class="crayon-plain-tag">CALL Employee.usp_InsertEmployeeDetails (1,'Anvesh','anvesh@gmail.com');
CALL Employee.usp_InsertEmployeeDetails (1,'Roy','Roy@gmail.com');</pre>
<p>Above are the two different calls with same EmpID value. The first call executes without any error message and the second call execute with an error message.</p>
<p><b>The resule of Second Call:</b></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/05/Continue1.png"><img decoding="async" class=" size-full wp-image-165 aligncenter" src="https://www.dbrnd.com/wp-content/uploads/2015/05/Continue1.png" alt="Continue1" width="225" height="194" srcset="https://www.dbrnd.com/wp-content/uploads/2015/05/Continue1.png 225w, https://www.dbrnd.com/wp-content/uploads/2015/05/Continue1-150x129.png 150w" sizes="(max-width: 225px) 100vw, 225px" /></a></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/05/Continue2.png"><img fetchpriority="high" decoding="async" class="aligncenter wp-image-166 size-full" src="https://www.dbrnd.com/wp-content/uploads/2015/05/Continue2.png" alt="Continue2" width="308" height="194" srcset="https://www.dbrnd.com/wp-content/uploads/2015/05/Continue2.png 308w, https://www.dbrnd.com/wp-content/uploads/2015/05/Continue2-300x189.png 300w" sizes="(max-width: 308px) 100vw, 308px" /></a></p>
<p>As we defined CONTINUE, so you can find two results in above image. One is our custom error message and second is the result of the defined SELECT statement.<br />
The execution didn&#8217;t stop by error, and it continued for another part.</p>
<p><b>Now, check the EXIT handler:</b></p>
<p><b>Please modify your handler and replace CONTINUE by EXIT:</b></p><pre class="crayon-plain-tag">DELIMITER //
CREATE PROCEDURE Employee.usp_InsertEmployeeDetails
(
     InputEmpID INTEGER
    ,InputEmpName VARCHAR(50)
    ,InputEmailAddress VARCHAR(50)    
)
/*****************************************************************
Authors Name : Anvesh Patel
Created Date : 2015-05-20
Description : This is demo stored procedure to 
              insert record into table with proper 
              error handling.Basically for www.dbrnd.com readers. 
******************************************************************/
BEGIN 
    DECLARE EXIT HANDLER FOR SQLEXCEPTION SELECT 'Error occured';
 INSERT INTO Employee.tbl_EmployeeDetails
 (
  EmpID
  ,EmpName 
  ,EmailAddress  
 )
 VALUES
 (
  InputEmpID
  ,InputEmpName 
  ,InputEmailAddress   
 );    
 SELECT *FROM Employee.tbl_EmployeeDetails;
END 
// DELIMITER ;</pre><p>
<p><b>Call with the same parameter:</b></p><pre class="crayon-plain-tag">CALL Employee.usp_InsertEmployeeDetails (1,'Roy','Roy@gmail.com');</pre><p>
<p><b>The Result is an only error message, and you cannot find two results as we defined EXIT to exit the code when an error occurred.</b></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/05/Exit1.png"><img decoding="async" class="aligncenter wp-image-167 size-full" src="https://www.dbrnd.com/wp-content/uploads/2015/05/Exit1.png" alt="Exit1" width="218" height="174" /></a></p>
<blockquote>
<p><b>The best practice is to create a output parameter and store 1 if any error occurred.</b></p>
<p><b>Application code has to check this output parameter is NULL or 1.</b></p>
<p><b>1 = Error.</b></p>
<p><b>NULL = No Error.</b></p>
<p><b>Below is a stored procedure for this:</b></p></blockquote>
<pre class="crayon-plain-tag">DELIMITER //
CREATE PROCEDURE Employee.usp_InsertEmployeeDetails

(
     InputEmpID INTEGER
    ,InputEmpName VARCHAR(50)
    ,InputEmailAddress VARCHAR(50)
    ,out IsError INTEGER 
)
/***********************************************************
Authors Name : Anvesh Patel
Created Date : 2015-05-20
Description : This is demo stored procedure to insert 
              record into table with proper error handling.
              Basically for www.dbrnd.com readers. 
************************************************************/
BEGIN 
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET IsError=1;    

 INSERT INTO Employee.tbl_EmployeeDetails
 (
  EmpID
  ,EmpName 
  ,EmailAddress  
 )
 VALUES
 (
  InputEmpID
  ,InputEmpName 
  ,InputEmailAddress   
 );    
 SELECT *FROM Employee.tbl_EmployeeDetails;
END 
// DELIMITER ;</pre>
<p><strong>Now call the above SP and select output parameter:</strong></p><pre class="crayon-plain-tag">CALL Employee.usp_InsertEmployeeDetails (1,'Roy','Roy@gmail.com',@IsError);
SELECT @IsError;</pre><p>
<p><b>Now Results are:</b></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/05/IsError1.png"><img loading="lazy" decoding="async" class=" size-full wp-image-168 aligncenter" src="https://www.dbrnd.com/wp-content/uploads/2015/05/IsError1.png" alt="IsError1" width="196" height="190" /></a></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/05/IsError2.png"><img loading="lazy" decoding="async" class=" size-full wp-image-169 aligncenter" src="https://www.dbrnd.com/wp-content/uploads/2015/05/IsError2.png" alt="IsError2" width="290" height="191" /></a></p>
<p><b>Above is a simple demonstration of Error Handling in MySQL. You can also use SQLSTATE which shows default error messages of MySQL.</b></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/05/mysql-error-handling/feed/</wfw:commentRss>
			<slash:comments>20</slash:comments>
		
		
			</item>
	</channel>
</rss>
