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

  • Home
  • SEARCH
  • 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 3975790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:43:38+00:00 2026-05-20T04:43:38+00:00

So I have this MS SQL Stored Procedure: ALTER PROCEDURE [dbo].[Import_Agent_Client_Bucket_2010] AS BEGIN —

  • 0

So I have this MS SQL Stored Procedure:

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 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;

    WHILE @@FETCH_STATUS = 0
        BEGIN
            -- 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

            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

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

                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]) 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
                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

                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 GUID: '
                PRINT @Client_GUID

                FETCH NEXT FROM Client_Cursor
                INTO @Client_ID;
            END;

            CLOSE Client_Cursor;
            DEALLOCATE Client_Cursor;

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

END

But I get an error message on just a very few of the items, it says

Msg 2627, Level 14, State 1, Procedure
Import_Agent_Client_Bucket_2010, Line
71 Violation of PRIMARY KEY constraint
‘Client_Bucket_Client_PK’. Cannot
insert duplicate key in object
‘dbo.Client_Bucket_Client’. The
statement has been terminated.

  • 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-20T04:43:39+00:00Added an answer on May 20, 2026 at 4:43 am

    EDIT:

    OK, I see what you’re doing there, I apologize for missing the OUTPUT statement. Based on that information, it seems like the code could break if a record is not inserted into the Client table in the line right before SELECT @Client_GUID = myNewPK FROM @myNewPKTable. If no record is inserted, you would wind up grabbing the GUID from the previous record and when you go to insert that it would cause the PK violation. You might have to check to make sure that records are being inserted into the Client table.

    ORIGINAL ANSWER:

    It looks like you’re declaring a table:

    DECLARE @myNewPKTable TABLE (myNewPK UNIQUEIDENTIFIER)
    

    But then you never put anything into it, so this statement must return null:

    SELECT @Client_GUID = myNewPK FROM @myNewPKTable
    

    EDIT:

    Why not just do this? I don’t see why the table @myNewPKTable is even being created.

    SET @Client_GUID = NEWID()
    

    EDIT:

    I think the reason you are getting the primary key violation is because @Client_Bucket_GUID is null. At the beginning of the procedure, there is this code:

    -- Create a bucket for each agent
    DECLARE @cbPKTable TABLE (cbPK UNIQUEIDENTIFIER, cbID int) 
    
    ...
    
    DECLARE @Client_Bucket_GUID uniqueidentifier
    SELECT @Client_Bucket_GUID = cbPK FROM @cbPKTable
    

    After this code is run @Client_Bucket_GUID will always be null. Again, you would have to insert records into @cbPKTable if you wanted to get anything out of it. If you’re trying to create a new UNIQUEIDENTIFIER and store it in @Client_Bucket_GUID, just use the NEWID() function.

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

Sidebar

Related Questions

I have the following stored procedure ALTER PROCEDURE [dbo].Test AS BEGIN CREATE TABLE ##table
--Stored procedure ALTER PROCEDURE [dbo].[Test] @USERID varchar(25) AS BEGIN SET NOCOUNT ON IF NOT
I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
Say I have this great query in my stored procedure. Select * from Temp
Created a stored procedure in SQL 9 (2005) and have since upgraded to SQL
I tried changing a default parameter value with this: ALTER PROCEDURE [dbo].[my_sp] @currentDate datetime
I have created a stored procedure shown below ,how will i call this from
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
I have this recorded in SQL Server: 1- startTime (datetime): 5/2/2009 08:30 (brazilian time

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.