Using the try Catch in SQL Server

Source: Internet
Author: User
Tags try catch

To create the error log table:

CREATE TABLE errorlogintNVARCHAR (NVARCHARintNVARCHAR( ))         
To create an error logging stored procedure:
CREATE PROCEDURE errorlogAs SELECTError_number()As Errnum,Error_severity()As Errsev,Error_state()As Errstate,Error_procedure()As Errproc,error_line () as errlineerror_message () as errmsg insert into errorlog error_number (), Span style= "Color:magenta;" >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 obtain the date of 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* from dboreplace ( Dayconvertdatetime,birthday and month convert,Brithday< Span style= "Color:gray;" >)), 0, ') [email protected] is 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!        

Using the try Catch in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.