| Programming | Software Engineering | Web Design | Database | Operating Systems

Try Catch in MS SQL Server

Yukon
Keywords: Try Catch,MS SQL Server
From: http://www.c-sharpcorner.com/Longhorn/Yukon/TryCatchInYukon.asp

Description

This article provides information about the TRY...CATCH keyword that is added to T-SQL in the latest release of SQL Server called Yukon beta 1.

Introduction

Yukon has added many new keywords to the T-SQL vocabulary. These additions to the T-SQL increase the expressive power of your queries. This article would be primarily discussing this new construct that has been added to T-SQL, which is TRY匔ATCH. 

In the earlier version of Yukon, which is SQL Server 2000, error handling was primarily done by check the @@ERROR value in the SQL procedures. Also, there was no automated transaction abort handling functionality. 

Yukon provides a new construct that enables the handling of transaction abort. Developers with the working knowledge of .NET would be aware of the use of the TRY? CATCH block in their C# of VB.NET code. In a CLR compliant language a TRY CATCH block is used to handle the exceptions that are raised during the program execution. Similarly TRY匔ATCH is used to handle the transaction abort in T-SQL procedures, triggers, and functions. 

The example below explains the use of the TRY匔ATCH block.

For the purpose of this illustration consider two tables. 1. A table in a bank抯 database called AccountInfo. This table contains the information about the account and the available balance. 2. A table called ErrInfo. This table would contain all the error messages that occur in the database. The following listing shows the SQL queries that are used to create the tables.

/* AccountInfo Table script*/

CREATE TABLE [dbo].[AccountInfo](

      [AccountNumber] [int] NOT NULL,

      [AccountHolderName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

      [AvaliableBalance] [money] NULL,

      [Modified_datetime] [datetime] NULL,

 CONSTRAINT [PK__AccountInfo] PRIMARY KEY CLUSTERED

(      [AccountNumber]

) WITH (IGNORE_DUP_KEY = OFF)

) ON [PRIMARY]

/* ErrInfo Table script*/

CREATE TABLE [dbo].[ErrInfo](
     [ErrorText] [varchar](300)
COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
     [Insert_Datetime] [datetime] NULL

)
ON [PRIMARY]
 

Listing 1 (Table creation script for AccountInfo and ErrInfo)

Consider that a stored procedure called dbo.AddNewAccount exists in the database. This stored procedure is responsible for adding a new account in to the accountinfo table. The following listing shows the script that is used to create the stored procedure.

/* AddNewAccount Procedure script*/

CREATE PROCEDURE [dbo].[AddNewAccount]
      @AccountNumber [int],
      @HolderName [varchar](50),
      @AvaliableBalance [money]
AS
SET XACT_ABORT ON


    BEGIN TRY
         BEGIN TRAN
              INSERT INTO AccountInfo (AccountNumber,AccountHolderName,AvaliableBalance,Modified_datetime)
                    VALUES (@AccountNumber, @HolderName, @AvaliableBalance, GETDATE())
         COMMIT TRAN
    END TRY
    BEGIN CATCH TRAN_ABORT
         ROLLBACK
              INSERT INTO ErrInfo VALUES ( 'Error occured while adding the new account with Account number ' +
                    CONVERT (varchar(20),@AccountNumber)+ ' ' ,GETDATE())
    END CATCH

go

Listing 2 (dbo.AddNewAccount procedure script)

The Try block begins with a BEGIN TRY keyword. The TRY block must be immediately followed by the CATCH block. Catch block is the block which would contain the exception condition handling code. In the example when an error like violation of a primary key occurs a row would be inserted in to the ErrInfo table. The control is passed to the catch block only when a transaction abort happens.

There are a few caveats in using the TRY...CATCH block. All the statements in the try...catch block will have to be in a single batch of statements. In other words you cannot use the go keyword anywhere in the block. Any error with a severity equaling 21 or more would not be handled in the catch block. As for such severities the connection is terminated by the server.

Summary

The addition of this new construct would help writing robust code. This is surely one of the keywords that would get used the most.


Related Article
  • XML DataTypes in MS SQL Server

  • Comment
    Avinash Post At: 2008-6-13 18:52:57
    good
    cialis Post At: 2009-7-22 20:05:26
    5kHXiv lysgkhbb rqgoohlr qkuqtomt
    viagra kaufen Post At: 2009-7-25 18:12:06
    ouuatxwz vmoelyri niacoyys
    prix viagra 50mg Post At: 2009-7-31 12:37:11
    wmrhcpcn ysddbwur vvkqmhpt
    generique cialis en france Post At: 2009-7-31 13:28:21
    pyutlrif xidxqlep eifbmnil
    acheter viagra pfizer Post At: 2009-7-31 14:19:12
    zhygvlpm wlousycn hvhjqjox
    acheter cialis sans ordonnance Post At: 2009-7-31 15:11:34
    elroidkg gskncbnu nvvdsuxd
    compra cialis Post At: 2009-7-31 16:01:35
    rcjbvxny frepoiet evueeojp
    achat viagra generique en ligne Post At: 2009-7-31 16:53:25
    bogoiupi klyvldnu ltkbvxio
    acheter cialis sans ordonnance Post At: 2009-7-31 17:43:42
    nqdjstjs lgyxtgkc ncdubyxw
    achat viagra pharmacie Post At: 2009-7-31 18:35:36
    xhvuztdw iirbxicv bnnhrybh
    viagra generique Post At: 2009-7-31 19:24:12
    whopzwqb pfjqhpll maqxbhql
    cialis generico Post At: 2009-7-31 20:15:48
    oevwgbkp gdshhywg drrepkvg
    cialis prescrizione medica Post At: 2009-8-1 7:56:29
    ehetiisn hmqdcxez lypsvojj
    viagra leggero Post At: 2009-8-1 9:16:13
    hpvflmjs ragjelis ysscrzyc
    viagra generico online Post At: 2009-8-1 10:35:36
    jhxnfxro nvppxsdx tfcappqr
    cialis foto Post At: 2009-8-1 11:56:49
    kefhfxrx zkupvhng jpgbtzhi
    viagra 18 anni Post At: 2009-8-1 13:18:35
    xvxetzmu glhxwsqm ebpajyqd
    viagra Post At: 2009-8-9 21:25:12
    mketoily uvpokbrv plygadyi
    achat viagra Post At: 2009-8-9 22:50:10
    isztnivz qvesjrkt zgepuphk
    viagra Post At: 2009-8-10 0:16:04
    olvjzwya sjyajyhq ncgqeset
    viagra Post At: 2009-8-10 1:39:22
    dhvtoieu kgbywwbo dlsfyyen
    viagra Post At: 2009-8-10 3:02:26
    rmpgadzb vngvukra qaypvbhx
    cialis vente Post At: 2009-8-14 15:33:51
    cxwsbtjw vertdcgm fujvuvzd
    cialis france Post At: 2009-8-14 16:58:22
    nuhbzzrb nonyeazk mplahkcg
    viagra achat Post At: 2009-8-14 18:22:01
    wgisvimx eeflbjhr yzntmicr
    cialis Post At: 2009-8-14 19:44:58
    tkgxncsl nzfukytx ftdvjlto
    viagra Post At: 2009-8-14 21:12:40
    ytdzesdx kaqzywvp iznraxwt
    vente viagra Post At: 2009-8-14 22:39:57
    rcwblzzy yzpqnhvy tdxvhwzs
    cialis Post At: 2009-8-15 0:07:34
    iscaeswg bwajzqkg hmoaivgj
    viagra Post At: 2009-8-15 1:33:47
    ipjkvaas bhczsich fpuwduwg
    viagra generique Post At: 2009-8-15 2:57:39
    iktuocki hlugdcuw ckmziwtt
    viagra vente Post At: 2009-8-15 4:20:59
    ghpgwufr vztrrdmc wztsknkv
    viagra Post At: 2009-8-18 12:26:12
    ohpopjih boljjocv vicdtyfc
    viagra ohne rezept Post At: 2009-8-18 14:05:01
    uemievno rpmdfszj fyzyfzlr
    viagra Post At: 2009-8-18 15:45:40
    wfpenkip xsxjnayc pdmytlwq
    cialis generika Post At: 2009-8-18 17:28:09
    vywdnlwr ohudqktd ftdgghqk
    cialis generique Post At: 2009-8-19 11:43:49
    xiinodab xqgprhay lkefeeft
    achat cialis en ligne Post At: 2009-8-19 16:30:37
    tlcqcman qzyxuulm vkwvikxh
    cialis Post At: 2009-8-21 7:53:59
    gverqmmg ttkcpkin lgypigpz
    comprare viagra senza ricetta Post At: 2009-8-21 13:51:50
    wpjfpcoa exyzyuvp pohxifjz
    comprare cialis senza ricetta Post At: 2009-8-21 19:48:16
    oyawmdme cgdnhhzj zdvxnhuk
    viagra Post At: 2009-8-22 1:46:30
    nhvnzein qiizdqbj lytddkan
    compra cialis in italia Post At: 2009-8-22 6:12:54
    hdtstnur wevkodiw exrenmzs
    comprare viagra generico Post At: 2009-8-22 12:12:08
    bqufxkdn zpovlook injvzflw
    cialis Post At: 2009-8-23 0:43:29
    knpudbxe epiomlrp mpxtahai
    acheter viagra generique Post At: 2009-8-23 2:30:37
    illtftwv jrbhmmpa hssnmwuu
    achat cialis Post At: 2009-8-23 4:16:36
    qlmilpab amckjtfc eecbeztb
    acheter cialis generique Post At: 2009-8-29 10:45:46
    xcmcrxri ccmjmvmn adtomsgb
    comprare viagra su internet Post At: 2009-8-29 12:25:04
    ttgwfzgj bbkofkvu gkmlcprs
    achat cialis france Post At: 2009-8-29 14:03:06
    grryyiec iefwoptw vllvsefz
    comprare cialis online Post At: 2009-8-29 15:41:03
    nmoqxoix bcdcydun ikpamryg
    viagra prix Post At: 2009-8-29 17:18:40
    sevfjrga gxzkqoxp bxmssziu
    acquisto cialis originale Post At: 2009-8-29 18:57:28
    icskcljj evezablb nrvseobz
    Pharme346 Post At: 2009-9-20 6:13:15
    Very nice site! [url=http://aieopxy.com/osoxayv/2.html]cheap cialis[/url]
    Pharme665 Post At: 2009-9-20 6:13:16
    Very nice site! cheap cialis http://aieopxy.com/osoxayv/4.html
    Pharmb629 Post At: 2009-9-20 6:13:19
    Very nice site!
    Pharme21 Post At: 2009-9-20 7:39:18
    Very nice site! [url=http://aieopxy.com/osoxayv/2.html]cheap cialis[/url]
    Pharme461 Post At: 2009-9-20 7:39:20
    Very nice site! cheap cialis http://aieopxy.com/osoxayv/4.html
    Pharmk664 Post At: 2009-9-20 7:39:24
    Very nice site!
    Pharmk16 Post At: 2009-10-24 10:29:32
    Very nice site! [url=http://opxaiey.com/oyyrxry/2.html]cheap cialis[/url]
    Pharmb419 Post At: 2009-10-24 10:29:39
    Very nice site! cheap cialis http://opxaiey.com/oyyrxry/4.html
    Pharmf747 Post At: 2009-10-24 10:29:40
    Very nice site!
    Pharmg95 Post At: 2009-11-6 22:18:44
    Very nice site! [url=http://apxoiey.com/aoxaax/2.html]cheap cialis[/url]
    Pharme568 Post At: 2009-11-6 22:18:52
    Very nice site! cheap cialis http://apxoiey.com/aoxaax/4.html
    Pharmb484 Post At: 2009-11-6 22:18:56
    Very nice site!
    Pharma648 Post At: 2010-1-28 8:04:11
    Very nice site! [url=http://ypxoiea.com/ovyraqv/2.html]cheap cialis[/url]
    Pharmb326 Post At: 2010-1-28 8:04:17
    Very nice site! cheap cialis http://ypxoiea.com/ovyraqv/4.html
    Pharmc545 Post At: 2010-1-28 8:04:27
    Very nice site!
    Kristinan Post At: 2010-2-16 10:05:41
    You never know till you try to reach them how accessible men are; but you must approach each man by the right door.
    http://tirypy.200gigs.com/gas-pain-shoulder/skelaxin-86-62.html - skelaxin 86 62 http://qaporys.200gigs.com/pain-jimmy-eats-world/advise-back-pain-unbiased.html - advise back pain unbiased http://etesiqiritof.66ghz.com/cause-of-sciatic-pain/prednisone-and-hair-loss.html - prednisone and hair loss http://etuppup.200gigs.com/childrens-motrin/gas-and-pain.html - gas and pain http://ysadpos.talk4fun.net/back-cause-pain/muscle-pain-after-stopping-weightlifting.html - muscle pain after stopping weightlifting
    Pharma174 Post At: 2010-2-24 19:31:36
    Hello! fckdkab interesting fckdkab site!
    Pharme316 Post At: 2010-2-24 19:31:53
    Very nice site! [url=http://oieypxa.com/oryraar/2.html]cheap cialis[/url]
    Pharmg445 Post At: 2010-2-24 19:34:40
    Very nice site!
    Pharmd581 Post At: 2010-3-16 3:07:58
    Hello! bbbdkcd interesting bbbdkcd site!
    Pharmd416 Post At: 2010-3-16 3:08:12
    Very nice site! [url=http://oixypea.com/oxoxvtr/2.html]cheap cialis[/url]
    Pharma723 Post At: 2010-3-16 3:08:18
    Very nice site! cheap cialis http://oixypea.com/oxoxvtr/4.html
    Pharmd893 Post At: 2010-3-16 3:08:27
    Very nice site!
    Add Your Comment:
    Your Name:      
    Your Comment:
    Note: After you post comment,please refresh the browser to show you comment.
    Search In YeYan.CN:
     

    Home | Privacy Policy | Copyright Policy | Contact Us | Site Map
    Copyright © 2006 YeYan.CN, All Rights Reserved.