Original: Use try Catch in SQL
Today in the Write SQL appeared! I want to use Trycatch in SQL! Hey.. But the grammar can't remember again! Then be clear about our Google Masters! Hey, there's really a lot of articles on the web about using try catch in SQL! There's less gossip! Write it down today so that you can check it next time!
To create the error log table:
Errorlog(int,NVARCHAR(+),int, NVARCHAR(+),INTNVARCHAR(+))
To create an error logging stored procedure:
CREATE PROCEDUREErrorlogAs SELECTError_number() asErrnum,error_severity() asErrsev,error_state() asErrstate,error_procedure() asErrproc,Error_line() asErrline,error_message() asErrMsgINSERT intoErrorlogVALUES(Error_number(),error_severity(),error_state(),error_procedure(),Error_line(),error_message())GO
Write a stored procedure! Inside use try Catch:
Use[Your_test] GO/****** object:storedprocedure [dbo]. [Gettodaybirthday] Script date:05/17/2010 15:38:46 Author:jinho Desc: A person who has been taken to the day of birth? ******/SET ansi_nulls onGOSET quoted_identifier onGOALTER PROCEDURE[dbo].[Gettodaybirthday]Asbegin TRY Declare@todaydatetime; SET@today= GETDATE();--to be taken to the present day?DECLARE@dayVARCHAR(2); SET@day=REPLACE( Day(@today),0,"'); DECLARE@monthVARCHAR(2) ; SET@month= REPLACE(Month(@today),0,"'); DECLARE@yearVARCHAR(4); SET@year= Year(@today); SELECT* fromDbo.UserInfoWHERE REPLACE( Day(CONVERT(DATETIME,Birthday) ), 0, ") =@dayand REPLACE(MONTH(CONVERT(DATETIME,Brithday) ), 0, ") =@month andBirthdayis not NULL
END CATCH Description:error_number(),error_severity(),error_state (), error_procedure (), Error_line (), error_message () These functions can only be used in catch!
Technorati Tags: sqlserver,sql,try catch
Using try Catch in SQL