Algo muy útil al momento de desarrollar procedimientos almacenados en Microsoft SQL es el manejo de errores:
DECLARE @var1 INT
DECLARE @var2 INT
DECLARE @resultado INT
BEGIN
BEGIN TRY
SET @var1 = 1
SET @var2 = 0
SET @resultado = @var1 / @var2
PRINT 'Excelente'
END TRY
BEGIN CATCH
PRINT 'ERROR_NUMBER ' + CAST(ERROR_NUMBER() AS VARCHAR)
PRINT 'ERROR_SEVERITY ' + CAST(ERROR_SEVERITY() AS VARCHAR)
PRINT 'ERROR_STATE ' + CAST(ERROR_STATE() AS VARCHAR)
PRINT 'ERROR_PROCEDURE ' + ISNULL(CAST(ERROR_PROCEDURE() AS VARCHAR),'N/A')
PRINT 'ERROR_LINE ' + CAST(ERROR_LINE() AS VARCHAR)
PRINT 'ERROR_MESSAGE ' + CAST(ERROR_MESSAGE() AS VARCHAR)
END CATCH
END |
declare @var1 int
declare @var2 int
declare @resultado int
begin
BEGIN TRY
set @var1 = 1
set @var2 = 0
set @resultado = @var1 / @var2
PRINT 'Excelente'
END TRY
BEGIN CATCH
PRINT 'ERROR_NUMBER ' + cast(ERROR_NUMBER() as varchar)
PRINT 'ERROR_SEVERITY ' + cast(ERROR_SEVERITY() as varchar)
PRINT 'ERROR_STATE ' + cast(ERROR_STATE() as varchar)
PRINT 'ERROR_PROCEDURE ' + ISNULL(cast(ERROR_PROCEDURE() as varchar),'N/A')
PRINT 'ERROR_LINE ' + cast(ERROR_LINE() as varchar)
PRINT 'ERROR_MESSAGE ' + cast(ERROR_MESSAGE() as varchar)
END CATCH
end
Analista/Desarrollador y cofundador de QBit Mexhico. Tecnologías utilizadas Android, Java (EJB, Servlets, Faces, Groovy & Grails, Maven, Swing), .Net (C#, VB, ASP, MVC), Delphi, Spring, Hibernate, DTSx, Oracle, T-SQL, Firebird, MySQL, MongoDB, NodeJs, ReactJs (Flux). Sistemas Operativos Mac, Ubuntu y Windows.