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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:49:08+00:00 2026-05-20T09:49:08+00:00

I have a stored procedure below. In it is a nested loop. Instead of

  • 0

I have a stored procedure below. In it is a nested loop. Instead of using FETCH_STATUS I want to use something else to mark my cursor as being finished, so that I can do a nested loop. But it’s never picking up @Client_ID in the cursor and I don’t know why.

ALTER PROCEDURE [dbo].[Import_Agent_Client_Bucket_2010]
AS
BEGIN
    -- Loop Through Each Agent, Create a Bucket, Add their Clients to the Bucket
    DECLARE Agent_Cursor CURSOR FOR
    SELECT TOP 10 Agent_GUID, Agent_ID
    FROM  realforms_2011.dbo.Agent

    DECLARE @Agent_GUID uniqueidentifier
    DECLARE @Agent_ID int

    OPEN Agent_Cursor;
    FETCH NEXT FROM Agent_Cursor
    INTO @Agent_GUID, @Agent_ID;

    DECLARE @MaxAgentCount int
    SELECT @MaxAgentCount = COUNT(Agent_ID)
    FROM Agent

    DECLARE @AgentCounter int
    SET @AgentCounter = 0

    WHILE (1 < 2)
        BEGIN
            PRINT 'Agent Counter:'
            PRINT @AgentCounter

            SET @AgentCounter = @AgentCounter + 1
            -- Create a bucket for each agent
            DECLARE @cbPKTable TABLE (cbPK UNIQUEIDENTIFIER, cbID int) 

            INSERT INTO realforms_2011.dbo.Client_Bucket ([Description] ) OUTPUT inserted.Client_Bucket_GUID, inserted.Client_Bucket_ID INTO @cbPKTable
            SELECT ISNULL(a.First_Name, ' ') + ' ' + ISNULL(a.Last_Name, ' ') + '''s Clients'
            FROM  realforms_2011.dbo.Agent a
            WHERE Agent_GUID = @Agent_GUID

            DECLARE @Client_Bucket_GUID uniqueidentifier
            SELECT @Client_Bucket_GUID = cbPK FROM @cbPKTable

            PRINT 'Client Bucket GUID Inserted:'
            PRINT @Client_Bucket_GUID

            DECLARE @Client_Bucket_ID int
            SELECT @Client_Bucket_ID = cbID FROM @cbPKTable

            INSERT INTO realforms_2011.dbo.Agent_Client_Bucket (Agent_GUID, Agent_ID, Client_Bucket_GUID, Client_Bucket_ID)
            VALUES (@Agent_GUID, @Agent_ID, @Client_Bucket_GUID, @Client_Bucket_ID)

            DECLARE @Client_GUID uniqueidentifier
            DECLARE @Client_ID int

            -- Get clients from the server (2010)
            DECLARE Client_Cursor CURSOR FOR
            SELECT C.Client_ID
            FROM realforms.dbo.Client C
                INNER JOIN realforms.dbo.Agent_Client AC ON AC.Client_ID = C.Client_ID
            WHERE AC.Agent_ID = @Agent_ID 
            ORDER BY C.Client_ID ASC

            DECLARE @MaxClientCreateDate datetime

            SELECT @MaxClientCreateDate = MAX(Create_Date) 
            FROM realforms.dbo.Client C

            PRINT 'Max Create Date:'
            PRINT @MaxClientCreateDate

            DECLARE @ClientCreateDate datetime

            SET @ClientCreateDate = CAST('01-01-2000' AS datetime)

            OPEN Client_Cursor;
            FETCH NEXT FROM Client_Cursor
            INTO @Client_ID
            -- loop through each 2010 client
            WHILE (1 < 2)
            BEGIN
                DECLARE @myNewPKTable TABLE (myNewPK UNIQUEIDENTIFIER) 

                BEGIN TRY
                    PRINT 'Client ID:'
                    PRINT @Client_ID
                    PRINT 'Agent ID:'
                    PRINT @Agent_ID

                    INSERT INTO realforms_2011.dbo.Client (Client_ID,Name,Secondary_Name,[Address],Address_2,City_State_Zip,Phone,Email_Address,Secondary_Email_Address,Create_Date,Last_Change_Date,[Status],File_Under,[Year],IsFavorite) OUTPUT inserted.Client_GUID INTO @myNewPKTable
                    SELECT c.Client_ID,Name,Secondary_Name,[Address],Address_2,City_State_Zip,Phone,Email_Address,Secondary_Email_Address,Create_Date,Last_Change_Date,[Status],File_Under,2010,0
                    FROM realforms.dbo.Client C
                        INNER JOIN realforms.dbo.Agent_Client AC ON AC.Client_ID = C.Client_ID
                    WHERE AC.Agent_ID = @Agent_ID AND C.Client_ID = @Client_ID

                    SELECT @Client_GUID = myNewPK FROM @myNewPKTable

                    PRINT 'Client GUID Inserted: '
                    PRINT @Client_GUID              

                    INSERT INTO realforms_2011.dbo.Client_Bucket_Client (Client_Bucket_GUID, Client_GUID, Client_ID, Client_Bucket_ID, [Year])
                    VALUES (@Client_Bucket_GUID, @Client_GUID, @Client_ID, @Client_Bucket_ID, 2010)

                    PRINT 'Client Bucket GUID: '
                    PRINT @Client_Bucket_GUID
                    PRINT 'Client ID Inserted: '
                    PRINT @Client_ID

                    SELECT @ClientCreateDate = CAST(Create_Date as datetime)
                    FROM realforms.dbo.Client C
                        INNER JOIN realforms.dbo.Agent_Client AC ON AC.Client_ID = C.Client_ID
                    WHERE AC.Agent_ID = @Agent_ID AND C.Client_ID = @Client_ID
                    PRINT @ClientCreateDate

                END TRY
                BEGIN CATCH
                    PRINT 'Error:'
                    PRINT ERROR_MESSAGE()

                    PRINT 'Last Client GUID Inserted: '
                    PRINT @Client_GUID
                    PRINT 'Last Client ID: '
                    PRINT @Client_ID
                END CATCH

                IF @@ERROR != 0 GOTO ERR_HANDLER

                IF (@ClientCreateDate >= @MaxClientCreateDate)
                    BREAK

                FETCH NEXT FROM Client_Cursor
                INTO @Client_ID;
            END;

            CLOSE Client_Cursor;
            DEALLOCATE Client_Cursor;

            IF (@AgentCounter >= @MaxAgentCount)
                BREAK

            FETCH NEXT FROM Agent_Cursor
            INTO @Agent_GUID, @Agent_ID;
        END;
    CLOSE Agent_Cursor;
    DEALLOCATE Agent_Cursor;

ERR_HANDLER: PRINT 'ERROR'

END

It breaks on @Client_ID

  • 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-20T09:49:09+00:00Added an answer on May 20, 2026 at 9:49 am

    You can nest fetch_status since the last statement of each loop is fetch next.

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

Sidebar

Related Questions

I have a Stored Procedure that rolls-back a series of operations. I want to
Would someone please help me. I have a stored procedure (see below) for data
I'm using .NET 3.5 SP1. I have a stored procedure returning a result of
I have a stored procedure below, and I don't know if it's correct. I
I have a stored procedure (see below) which inserts data into a physical table
I have created a stored procedure shown below ,how will i call this from
I want to write a stored procedure that queries XML files after I have
I have designed a stored procedure usign Sql Server 2005 below to compare 3
I have an issue with the below stored procedure. It runs fine as long
I have stored procedure: ALTER PROCEDURE [dbo].[k_ShoppingCart_DELETE] @cartGUID nvarchar AS DELETE FROM [dbo].[k_ShoppingCart] WHERE

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.