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 787737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:15:08+00:00 2026-05-14T21:15:08+00:00

I am loading some data into a temp table. I then use a cursor

  • 0

I am loading some data into a temp table. I then use a cursor to loop through the temp table, minipulate the data, and insert it into another table. The identity of the newly inserted record is then captured in a variable and inserted to another table to allow a look up of the newly inserted data.

    DECLARE c1 CURSOR READ_ONLY
FOR
SELECT NEWClientIndex, ClientGroup, ClientAccount, SKAccount, SKDesc, SKBase, SKBranch, ClientType, SKStatus, GFCID, GFPID, Account_Open_Date, Account_Update, 
                      SKType
FROM @ClientsAccounts 

OPEN c1


FETCH NEXT FROM c1
INTO @NEWClientIndex, @ClientGroup, @ClientAccount, @SKAccount, @SKDesc, 
    @SKBase, @SKBranch, @ClientType, @SKStatus, @GFCID, @GFPID, @Account_Open_Date,
    @Account_Update, @SKType

WHILE @@FETCH_STATUS = 0
    BEGIN
PRINT 'Processing Account'

        Print 'Inserting Account Information'
        -- We now need to loop through the data inserting account information 
        -- and maintaining the Account link table
        -- Each itteration of the cursor should result in one insert
        -- as each account should be linked to one client

        INSERT INTO CitiClientsAccounts (SKBranch, SKAccount, SKName, SKBase, SyncStatus,
            GFCID, GFPID, SyncInput, SyncUpdate, Deleted, Branch_Account, LastUpdatedBy, AccountTypeID)
            VALUES(convert(varchar(2), @SKBranch), convert(varchar(12), @SKAccount), convert(varchar(255),@SKDesc), 
            convert(varchar(16),@SKBase), convert(varchar(50),@SKStatus), convert(varchar(10),@GFCID), convert(varchar(10),@GFPID),
            @Account_Open_Date, @Account_Update, 0, convert(varchar(16),@ClientAccount), 'Admin', convert(int, @SKType))


            Declare @NEWID int
            Select @NEWID = SCOPE_IDENTITY()
            IF @NEWID is NULL
                BEGIN
                    Print 'Account Insert Failed'
                END
            ELSE
                BEGIN

                    --Match up account to the Client ID
                    Declare @ClientID int
                    Select @ClientID = LocalPrimaryKey
                        from dbo.CitiClients_LIBRARY
                    Where AccessPimaryKey = @NEWClientIndex

                    Print 'Updating Library'
                    Insert into dbo.ClientAccountLink 
                        (AccountID, ClientID, LastUpdatedBy) 
                    Values
                        (@NEWID, @ClientID, 'Admin')

            END
            Print 'End Insert Account Information'
        -- Move to Next Row
        FETCH NEXT FROM c1
        INTO @NEWClientIndex, @ClientGroup, @ClientAccount, @SKAccount, @SKDesc, 
            @SKBase, @SKBranch, @ClientType, @SKStatus, @GFCID, @GFPID, @Account_Open_Date,
            @Account_Update, @SKType
    END
CLOSE c1
DEALLOCATE c1

At times the insert fails, I have no idea why the insert does fail. I am just wondering how I can display more detailed information on why the insert is actually failing?

  • 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-05-14T21:15:09+00:00Added an answer on May 14, 2026 at 9:15 pm

    try using a catch block with more some PRINTs:

    DECLARE @ErrorMessage nvarchar(400), @ErrorNumber int, @ErrorSeverity int, @ErrorState int, @ErrorLine int,@CatchMessage varchar(500)
    
    
    DECLARE c1 CURSOR READ_ONLY
    FOR
    SELECT NEWClientIndex, ClientGroup, ClientAccount, SKAccount, SKDesc, SKBase, SKBranch, ClientType, SKStatus, GFCID, GFPID, Account_Open_Date, Account_Update, 
                          SKType
    FROM @ClientsAccounts 
    
    OPEN c1
    
    
    FETCH NEXT FROM c1
    INTO @NEWClientIndex, @ClientGroup, @ClientAccount, @SKAccount, @SKDesc, 
        @SKBase, @SKBranch, @ClientType, @SKStatus, @GFCID, @GFPID, @Account_Open_Date,
        @Account_Update, @SKType
    
    WHILE @@FETCH_STATUS = 0
        BEGIN
    PRINT 'Processing Account'
    
            Print 'Inserting Account Information'
            -- We now need to loop through the data inserting account information 
            -- and maintaining the Account link table
            -- Each itteration of the cursor should result in one insert
            -- as each account should be linked to one client
            BEGIN TRY
            SET @CatchMessage='INSERT INTO CitiClientsAccounts'
            INSERT INTO CitiClientsAccounts (SKBranch, SKAccount, SKName, SKBase, SyncStatus,
                GFCID, GFPID, SyncInput, SyncUpdate, Deleted, Branch_Account, LastUpdatedBy, AccountTypeID)
                VALUES(convert(varchar(2), @SKBranch), convert(varchar(12), @SKAccount), convert(varchar(255),@SKDesc), 
                convert(varchar(16),@SKBase), convert(varchar(50),@SKStatus), convert(varchar(10),@GFCID), convert(varchar(10),@GFPID),
                @Account_Open_Date, @Account_Update, 0, convert(varchar(16),@ClientAccount), 'Admin', convert(int, @SKType))
    
                Declare @NEWID int
                Select @NEWID = SCOPE_IDENTITY(),@CatchMessage=null
                IF @NEWID is NULL
                    BEGIN
                        Print 'Account Insert Failed'
                    END
                ELSE
                    BEGIN
    
                        --Match up account to the Client ID
                        Declare @ClientID int
                        SET @CatchMessage='SELECT dbo.CitiClients_LIBRARY'
                        Select @ClientID = LocalPrimaryKey
                            from dbo.CitiClients_LIBRARY
                        Where AccessPimaryKey = @NEWClientIndex
    
                        SET @CatchMessage='Insert into dbo.ClientAccountLink '
                        Print 'Updating Library'
                        Insert into dbo.ClientAccountLink 
                            (AccountID, ClientID, LastUpdatedBy) 
                        Values
                            (@NEWID, @ClientID, 'Admin')
    
                END
                Print 'End Insert Account Information'
    
            -- Move to Next Row
            SET @CatchMessage='FETCH NEXT FROM c1'
            FETCH NEXT FROM c1
            INTO @NEWClientIndex, @ClientGroup, @ClientAccount, @SKAccount, @SKDesc, 
                @SKBase, @SKBranch, @ClientType, @SKStatus, @GFCID, @GFPID, @Account_Open_Date,
                @Account_Update, @SKType
            END TRY
            BEGIN CATCH
    
                IF XACT_STATE()!=0
                BEGIN
                    ROLLBACK TRANSACTION
                END
    
                --will echo back the complete original error message
                SELECT @ErrorMessage = N'Error %d, Line %d, Message: '+ERROR_MESSAGE(),@ErrorNumber = ERROR_NUMBER(),@ErrorSeverity = ERROR_SEVERITY(),@ErrorState = ERROR_STATE(),@ErrorLine = ERROR_LINE()
                RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState, @ErrorNumber,@ErrorLine)
    
                PRINT 'ERROR WHEN PROCESSING: '+ISNULL(@CatchMessage,'')
                PRINT '    @NEWClientIndex   ='+ISNULL(''''+CONVERT(varchar(max),@NEWClientIndex   )+'''','null')
                PRINT '    @ClientGroup      ='+ISNULL(''''+CONVERT(varchar(max),@ClientGroup      )+'''','null')
                PRINT '    @ClientAccount    ='+ISNULL(''''+CONVERT(varchar(max),@ClientAccount    )+'''','null')
                PRINT '    @SKAccount        ='+ISNULL(''''+CONVERT(varchar(max),@SKAccount        )+'''','null')
                PRINT '    @SKDesc           ='+ISNULL(''''+CONVERT(varchar(max),@SKDesc           )+'''','null')
                PRINT '    @SKBase           ='+ISNULL(''''+CONVERT(varchar(max),@SKBase           )+'''','null')
                PRINT '    @SKBranch         ='+ISNULL(''''+CONVERT(varchar(max),@SKBranch         )+'''','null')
                PRINT '    @ClientType       ='+ISNULL(''''+CONVERT(varchar(max),@ClientType       )+'''','null')
                PRINT '    @SKStatus         ='+ISNULL(''''+CONVERT(varchar(max),@SKStatus         )+'''','null')
                PRINT '    @GFCID            ='+ISNULL(''''+CONVERT(varchar(max),@GFCID            )+'''','null')
                PRINT '    @GFPID            ='+ISNULL(''''+CONVERT(varchar(max),@GFPID            )+'''','null')
                PRINT '    @Account_Open_Date='+ISNULL(''''+CONVERT(varchar(max),@Account_Open_Date)+'''','null')
                PRINT '    @Account_Update   ='+ISNULL(''''+CONVERT(varchar(max),@Account_Update   )+'''','null')
                PRINT '    @SKType           ='+ISNULL(''''+CONVERT(varchar(max),@SKType           )+'''','null')
                PRINT '    @NEWID            ='+ISNULL(''''+CONVERT(varchar(max),@NEWID            )+'''','null')
                PRINT '    @ClientID         ='+ISNULL(''''+CONVERT(varchar(max),@ClientID         )+'''','null')
    
                --pick one, your way to terminate:
                EXIT --exit loop
                RETURN --exit procedure
                GOTO TheEnd --jump to label
    
            END CATCH
        END
    CLOSE c1
    DEALLOCATE c1
    
    TheEnd:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 392k
  • Answers 393k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use SubWCRev for this. By integrating it into… May 15, 2026 at 1:52 am
  • Editorial Team
    Editorial Team added an answer You are never locking a value - you are locking… May 15, 2026 at 1:52 am
  • Editorial Team
    Editorial Team added an answer Take a look how Watir unit tests are executed. May 15, 2026 at 1:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.