{"id":652,"date":"2017-08-05T00:48:42","date_gmt":"2017-08-05T00:48:42","guid":{"rendered":"http:\/\/www.manualsqlserver.com\/?p=652"},"modified":"2020-05-20T18:10:22","modified_gmt":"2020-05-20T18:10:22","slug":"funciones-para-control-de-errores","status":"publish","type":"post","link":"https:\/\/manualsqlserver.com\/?p=652","title":{"rendered":"Funciones para control de errores SQL Server"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"220\" height=\"252\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2020\/05\/ManualSQL_FuncionesErrores_D.png\" alt=\"\" class=\"wp-image-1320\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Funciones para el manejo de errores<\/strong><\/h2>\n\n\n\n<p>Las funciones para el manejo de errores permiten conocer los par\u00e1metros\u00a0que reporta un error, las funciones son las siguientes:<\/p>\n\n\n\n<!--more-->\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Funci\u00f3n<\/strong><\/td><td><strong>Descripci\u00f3n<\/strong><\/td><\/tr><tr><td>ERROR_NUMBER()<\/td><td>Devuelve el n\u00famero de error.<\/td><\/tr><tr><td>ERROR_SEVERITY()<\/td><td>Devuelve la severidad del error.<\/td><\/tr><tr><td>ERROR_STATE()<\/td><td>Devuelve el estado del error.<\/td><\/tr><tr><td>ERROR_PROCEDURE()<\/td><td>Devuelve el nombre del procedimiento almacenado que ha provocado el error.<\/td><\/tr><tr><td>ERROR_LINE()<\/td><td>Devuelve el n\u00famero de l\u00ednea en el que se ha producido el error.<\/td><\/tr><tr><td>ERROR_MESSAGE()<\/td><td>Devuelve el mensaje de error.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Ejemplo<\/strong><br><strong>El siguiente ejemplo muestra una divisi\u00f3n entre CERO, lo que arroja error, luego se muestran los valores de cada funci\u00f3n. Se utiliza la estructura Try Catch cuya explicaci\u00f3n est\u00e1 al final de este art\u00edculo.<\/strong><\/p>\n\n\n\n<p>BEGIN TRY<br>DECLARE @Valor1 Numeric(9,2),@Valor2 Numeric(9,2), @Division Numeric(9,2)<br>SET @Valor1 = 100<br>SET @Valor2 = 0<br>SET @Division = @Valor1\/@Valor2<br>PRINT &#8216;La divisi\u00f3n no reporta error&#8217;<br>END TRY<br>BEGIN CATCH<br>select ERROR_NUMBER() As &#8216;N\u00ba de Error&#8217;, ERROR_SEVERITY() As &#8216;Severidad&#8217;,<br>ERROR_STATE() As &#8216;Estado&#8217;, ERROR_PROCEDURE() As &#8216;Procedimiento&#8217;, ERROR_LINE() As &#8216;N\u00ba l\u00ednea&#8217;,<br>ERROR_MESSAGE() As &#8216;Mensaje&#8217;<br>END CATCH<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2020\/05\/ManualSQL_Errores_00.png\" alt=\"\" class=\"wp-image-1321\" width=\"844\" height=\"133\" srcset=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2020\/05\/ManualSQL_Errores_00.png 860w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2020\/05\/ManualSQL_Errores_00-300x47.png 300w, https:\/\/manualsqlserver.com\/wp-content\/uploads\/2020\/05\/ManualSQL_Errores_00-768x121.png 768w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>La Funci\u00f3n @@ERROR<\/strong><\/h3>\n\n\n\n<p>La funci\u00f3n @@ERROR almacena el n\u00famero de error producido por la \u00faltima sentencia Transact SQL ejecutada, si no se ha producido ning\u00fan error el valor de la funci\u00f3n es CERO.<br>Se puede usar esta funci\u00f3n para controlar los errores usando una estructura If<\/p>\n\n\n\n<p><strong>Ejemplo:<\/strong><br><strong>El siguiente ejemplo muestra una divisi\u00f3n entre CERO, lo que arroja error, luego se da consistencia al error con una estructura If<\/strong><\/p>\n\n\n\n<p>DECLARE @Valor1 Numeric(9,2),@Valor2 Numeric(9,2), @Division Numeric(9,2)<br>SET @Valor1 = 100<br>SET @Valor2 = 0<br>SET @Division = @Valor1\/@Valor2<br>If @@ERROR = 0<br>Begin<br>Print &#8216;El resultado es: &#8216; + Str(@Division)<br>Print &#8216;No hubo error&#8217;<br>End<br>Else<br>Begin<br>Print &#8216;Error al dividir entre CERO&#8217;<br>End<br>go<\/p>\n\n\n\n<p><strong>Probamos el mismo c\u00f3digo con el Valor2 igual a 2<\/strong><\/p>\n\n\n\n<p>DECLARE @Valor1 Numeric(9,2),@Valor2 Numeric(9,2), @Division Numeric(9,2)<br>SET @Valor1 = 100<br>SET @Valor2 = 2<br>SET @Division = @Valor1\/@Valor2<br>If @@ERROR = 0<br>Begin<br>Print &#8216;El resultado es: &#8216; + Str(@Division)<br>Print &#8216;No hubo error&#8217;<br>End<br>Else<br>Begin<br>Print &#8216;Error al dividir entre CERO&#8217;<br>End<br>go<\/p>\n\n\n\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-2636008503986218\"\n     data-ad-slot=\"1902193434\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Funciones para el manejo de errores Las funciones para el manejo de errores permiten conocer los par\u00e1metros\u00a0que reporta un error, las funciones son las siguientes:<\/p><p><a class=\"more-link btn\" href=\"https:\/\/manualsqlserver.com\/?p=652\">Seguir leyendo<\/a><\/p>\n","protected":false},"author":1,"featured_media":1320,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[113,27,112],"class_list":["post-652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-funcionessql","tag-error-2","tag-error","tag-funciones-de-error","nodate","item-wrap"],"_links":{"self":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=652"}],"version-history":[{"count":1,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/652\/revisions"}],"predecessor-version":[{"id":1325,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/652\/revisions\/1325"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/media\/1320"}],"wp:attachment":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}