Use try Catch (GO) in SQL Server

Source: Internet
Author: User
Tags try catch

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:

CREATE TABLE errorlog (errnum Int,errsev NVARCHAR (+), errstate int,errproc NVARCHAR (+), Errline INT, ErrMsg NVARCHAR (2000))
To create an error logging stored procedure:
CREATE PROCEDURE errorlogas      SELECT error_number () as errnum,error_severity () as Errsev,error_state () as Errstate, Error_procedure () as Errproc,error_line () as Errline,error_message () as ErrMsg      INSERT into      errorlog      VALUES (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: To be taken? When is the day of        birth? /set ansi_nulls ongoset quoted_identifier ongoalter PROCEDURE [dbo]. [Gettodaybirthday] Asbegin TRY declare @today datetime;  SET @today = GETDATE ();--------------------the day of the day? DECLARE @day VARCHAR (2); SET @day =replace (Day (@today), 0, "); DECLARE @month VARCHAR (2); SET @month = REPLACE (Month (@today), 0, '); DECLARE @year VARCHAR (4); SET @year = year (@today); SELECT * FROM dbo. UserInfo  
End TRY BEGIN Catch errorlog--call the stored procedure above to save the error log END CATCH   Description: Error_number (), error_severity (), Error_state (), Error_ PROCEDURE (), Error_line (), error_message () These functions can only be used in catch!

Use try Catch (GO) 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.