Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 1st, 2009, 09:56 AM
Authorized User
 
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
Default doubt in try catch functionality

Hi All

I have written a stored procedure where i am opening an xml and closing the xml after doing the transaction.
Here is the stored procedure
Code:
ALTERPROCEDURE [dbo].[HR_SP_SWIPE_REGULARIZATION_APPROVAL](@SessionId numeric(7),@Mode varchar(50),
@SwipeXml text,@FrwdAuthority numeric(7),
@ActionType varchar(30))
AS
DECLARE @hdoc int
IF(@Mode ='UPDATE')
BEGIN
IF(@ActionType ='APPROVE')
BEGINTRY
BEGINTRANSACTION-- Beginning the transaction
EXECSP_XML_PREPAREDOCUMENT @hdoc OUTPUT,@SwipeXml -- Preparing the XML document
SELECT EMPNO,REFNO,NONSWIP_DATE,REMARKSPM,SLNO
INTO #TEMPSWIPEAPPROVE 
FROMOPENXML(@hDoc,'/NewDataSet/APPROVE',2)
WITH(EMPNO NUMERIC(7),REFNO NUMERIC(7),NONSWIP_DATE VARCHAR(10),REMARKSPM VARCHAR(50),SLNO NUMERIC(7))--select * from #TEMPSWIPEAPPROVE
UPDATE ATTENDANCE_DETAILS 
SET 
STATUS ='AA',
REMARKSPM = A.REMARKSPM,
APPROVED_DATE =GETDATE(),
MODIFIED_DATE =GETDATE(),
TRANS_STAT ='Y',
MODIFIED_BY = @SessionId
FROM(SELECT*FROM #TEMPSWIPEAPPROVE)A
WHERE ATTENDANCE_DETAILS.REFNO = A.REFNO 
AND ATTENDANCE_DETAILS.SLNO = A.SLNO
AND ATTENDANCE_DETAILS.EMPNO = A.EMPNO 
AND ATTENDANCE_DETAILS.NONSWIP_DATE = A.NONSWIP_DATE

--To update swipe details
UPDATE SWIPE_DETAILS SET LEAVE_STATUS='AA',REMARKS='Regularization Approved',
MODIFIED_DATE =GETDATE(),
MODIFIED_STATUS=CASEWHEN MODIFIED_STATUS='NA'THEN'NR'
WHEN MODIFIED_STATUS='AN'THEN'RN'
WHEN MODIFIED_STATUS='XA'THEN'XR'
WHEN MODIFIED_STATUS='AX'THEN'RX'
ELSE'RR'END
FROM(SELECT*FROM #TEMPSWIPEAPPROVE)A
WHERE SWIPE_DETAILS.CARDID=A.EMPNO AND 
SWIPE_DETAILS.ATTNDATE=CAST(A.NONSWIP_DATE ASDATETIME)
AND A.REFNO=REFNO


COMMITTRANSACTION-- Committing the transaction
EXECSP_XML_REMOVEDOCUMENT @hdoc -- Removing the xml document
DROPTABLE #TEMPSWIPEAPPROVE
ENDTRY
-- ERROR HANDLING
BEGINCATCH
--IF @@TRANCOUNT > 0
ROLLBACKTRANSACTION-- Rolling back the transaction
EXECSP_XML_REMOVEDOCUMENT @hdoc -- Removing the XML document
-- Raise an error with the details of the exception
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
SET @ErrMsg = @ErrMsg + @SessionId
RAISERROR(@ErrMsg, @ErrSeverity, 1)
IFOBJECT_ID('#TEMPSWIPEAPPROVE')ISNOTNULL
DROPTABLE #TEMPSWIPEAPPROVE
ENDCATCH
Doubts:
1) Here if the error occurs in try block, then after entering into catch block, will it remove the xml document or not? or xml document automatically gets removed when error occurs in try block.
2) If any table gets locked in try block, supposingly here in this case swipe details table then how to handle this dead lock condition so that sql server time out error doesn't happen.(Please suppose that this table swipe_details is getting a record no. of hits.)

-- Please throw some light.

-- Abhishek





Similar Threads
Thread Thread Starter Forum Replies Last Post
Try, Catch Kevin7777 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 13 January 11th, 2010 06:09 AM
doubt in Catch block abhishekkashyap27 SQL Server 2005 0 April 21st, 2009 09:22 AM
Try...Catch lowell VB.NET 3 July 23rd, 2007 06:35 AM
Try and Catch? mujju PHP How-To 2 January 20th, 2005 12:27 PM
To Catch a User SerranoG Access 8 October 3rd, 2004 05:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.