Closing Cursor by Checking if exists in SQL Server
IF (SELECT CURSOR_STATUS('global','Cur_Name')) >=0
BEGIN
DEALLOCATE Cur_UDD
END
BEGIN
DEALLOCATE Cur_UDD
END
Ex :
BEGIN TRANSACTION
BEGIN TRY
DECLARE Cur_Name CURSOR FOR SELECT COL1,COL2 FROM TABLE
OPEN Cur_Name
FETCH NEXT FROM Cur_Name INTO @Col1,@Col2
WHILE @@FETCH_STATUS=0
BEGIN
FETCH NEXT FROM Cur_Name INTO @Col1,@Col2
END
CLOSE Cur_Name
DEALLOCATE Cur_Name
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
IF (SELECT CURSOR_STATUS('global','Cur_UDD')) >=0
BEGIN
DEALLOCATE Cur_UDD
END
DECLARE @Err_Msg NVARCHAR(MAX)
DECLARE @Err_Sev INT
SELECT @Err_Msg=ERROR_MESSAGE(),@Err_Sev=ERROR_SEVERITY()
RAISERROR(@Err_Msg,16,1)
END CATCH
BEGIN TRY
DECLARE Cur_Name CURSOR FOR SELECT COL1,COL2 FROM TABLE
OPEN Cur_Name
FETCH NEXT FROM Cur_Name INTO @Col1,@Col2
WHILE @@FETCH_STATUS=0
BEGIN
FETCH NEXT FROM Cur_Name INTO @Col1,@Col2
END
CLOSE Cur_Name
DEALLOCATE Cur_Name
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
IF (SELECT CURSOR_STATUS('global','Cur_UDD')) >=0
BEGIN
DEALLOCATE Cur_UDD
END
DECLARE @Err_Msg NVARCHAR(MAX)
DECLARE @Err_Sev INT
SELECT @Err_Msg=ERROR_MESSAGE(),@Err_Sev=ERROR_SEVERITY()
RAISERROR(@Err_Msg,16,1)
END CATCH
No comments:
Post a Comment