Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:22:38+00:00 2026-06-11T08:22:38+00:00

I have a fairly complex SP (logic wise) with nested IF BEGIN END ELSE

  • 0

I have a fairly complex SP (logic wise) with nested IF BEGIN END ELSE, there are multiple end points in this nested logic where the logic fails and I RAISERROR and two places that result in success and SQL being actioned.

How, at the end of the SP can I trap the errors and do a IF Error Count>0 THEN ROLLBACK

DECLARE @errCount int=0
DECLARE @DataSetCount int=0
DECLARE @countCurrent int=0
DECLARE @countHistorical int=0


IF (isnumeric(@DataSetID)=(0) OR @DataSetID=(0)) 
BEGIN
    RAISERROR('The DataSet specfied does not appear to be valid', 5, 1)
END
ELSE
IF  (@Destination='C' OR @Destination='H') 
    BEGIN
        if Exists (SELECT NULL from tblOpportunityDataSets where DataSetID=@DataSetID)
        BEGIN
            SET @countCurrent=(SELECT COUNT(1) from tblOptyRecordsCurrent where DataSetID=@DataSetID)
            SET @countHistorical=(SELECT COUNT(1) from tblOptyRecordsHistorical where DataSetID=@DataSetID)
            IF @destination='C'
            BEGIN
                if @countCurrent>0 
                BEGIN
                    RAISERROR('There are already existing records in the Current Tables for the specified DataSet', 5, 1)
                END ELSE
                if @countHistorical=0 
                BEGIN
                    RAISERROR('There do not appear to be any records in the Historical Tables to transfer for the specified Dataset', 5, 1)
                END ELSE
                BEGIN
                    -- ENTER TRANSFER CODE
                    INSERT INTO tblRecordsHistorical
                    ( X, Y, Z ) 
                    SELECT X, Y, Z  FROM tblA
                    WHERE x=y
                    -- Check that record count in both tables match
                    SET @countCurrent=(SELECT COUNT(1) from tblOptyRecordsCurrent where DataSetID=@DataSetID)
                    SET @countHistorical=(SELECT COUNT(1) from tblOptyRecordsHistorical where DataSetID=@DataSetID)
                    IF (@countCurrent<>@countHistorical)
                    BEGIN
                        RAISERROR('There was an error whilst copying the records into the Historical Tables, Source and Destination Record Count do not match', 5, 1)
                    END ELSE
                    BEGIN


                    END
                END
            END ELSE
            IF @Destination='H'
            BEGIN
                if @countHistorical>0 
                BEGIN
                    RAISERROR('There are already existing records in the Historical Tables for the specified DataSet', 5, 1)
                END ELSE
                if @countCurrent=0 
                BEGIN
                    RAISERROR('There do not appear to be any records in the Historical Tables to transfer for the specified Dataset', 5, 1)
                END ELSE
                BEGIN
                    RAISERROR('DataSet Found, ready to transfer records to HISTORICAL', 5, 1)       
                    -- ENTER TRANSFER CODE
                    INSERT INTO tblOptyRecordsCurrent
                    ( X, Y, Z ) 
                    SELECT X, Y, Z  FROM tblB
                    WHERE x=y
                    -- Check that record count in both tables match
                    SET @countCurrent=(SELECT COUNT(1) from tblOptyRecordsCurrent where DataSetID=@DataSetID)
                    SET @countHistorical=(SELECT COUNT(1) from tblOptyRecordsHistorical where DataSetID=@DataSetID)



                END
            END
        END
        ELSE
        BEGIN
            RAISERROR('The DataSet you have specified cannot be found', 5, 1)
        END
    END
    ELSE
    BEGIN
        RAISERROR('You have not specified a valid Destination', 5, 1)
    END

Note that in the two sections that contain the INSERT INTO code, there will be at least two additional SQL Action Statements, which must all work or they all fail.

EDIT: I’ve gone with

BEGIN TRAN
BEGIN TRY
    -- STATEMENT 1
    INSERT INTO X WHERE Y
    -- STATEMENT 2
    DELETE FROM X WHERE Y
    -- STATEMENT 3
    UPDATE X WHERE Y
    COMMIT
END TRY
BEGIN CATCH
    ROLLBACK TRAN
    RAISERROR('There was an error whilst copying the records into the Current Tables.  The Transaction has been rolled back', 5, 1)
END CATCH                               
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:22:40+00:00Added an answer on June 11, 2026 at 8:22 am

    In order to be able to do a rollback, you need to do a begin transaction at the start.

    Then you either commit or rollback the actions.

    http://msdn.microsoft.com/en-us/library/ms174377

    You may find the try/catch syntax easier

    http://msdn.microsoft.com/en-us/library/ms175976(v=sql.90).aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly complex html form enhanced via jquery. It has multiple tabs,
I have a fairly complex view that has multiple forms, lots of validations on
I have a fairly complex nested object, for example: A = {b : {c
Do you have any suggestion for this particular problem. I have fairly complex html
I have a fairly complex query that looks something like this: create table Items(SomeOtherTableID
I have a fairly complex Silverlight banner, with interstitials, hovers, active areas, etc. This
I have a fairly complex application at this point, and feel as though the
I have a fairly complex query in which I am working with with multiple
I have a fairly complex view that points back to other pages within my
I have a fairly complex application which has been broken up into multiple components.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.