TSQL – Manejo de Errores

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

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.

Twitter LinkedIn 

Compartir en...Tweet about this on TwitterPin on Pinterest0Share on LinkedIn0Share on Google+0Share on Facebook0