C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Exception handing (TRY/CATCH) in SQL SERVER 2005


Posted Date: 02 Oct 2008    Resource Type: Code Snippets    Category: SQL

Posted By: Vivek       Member Level: Gold
Rating:     Points: 10



In SQl Server 2005, @@Error is not required after every statement.

SQL Server 2005 now provide Try/Catch block.

Your main code will be part of Try block and then error handling routine will be inside catch blocK.

SO if code inside your try block fail then control goes to enclosing catch block, so that transaction can be rolled `back.

ERROR_NUMBER - return error number
ERROR_MESSAGE - return correspoding error message (reason of failure)

You can return any of these most used functions from catch block


Usage :

BEGIN TRY
-- SQL Code
END TRY

BEGIN CATCH
-- SQL Code
END CATCH


CREATE PROCEDURE [dbo].[p_acct_delete]

-- input parameter
@isn_num varchar(10)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ErrorCode VARCHAR(500)
set @ErrorCode = '0'

BEGIN TRY
DELETE FROM emplr WHERE isn_num=@isn_num
IF @@ROWCOUNT = 0
set @ErrorCode = '6002'
-- Error Code based on that you will be showing the exact message in UI
END TRY
BEGIN CATCH
SELECT @ErrorCode = ERROR_MESSAGE()
END CATCH
SELECT @ErrorCode
-- This will return your error code
RETURN
END
GO

			



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
SQL Server 2005 TRY CATCH Exception Handling  .  Exception handling (TRY/CATCH)  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sequence in SQL Server
Previous Resource: To Calculate 30 Working days from the Input Date
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use