带有while循环的T-SQL存储过程导致主键约束中的错误

时间:2011-02-19 21:43:30

标签: sql sql-server tsql stored-procedures

所以我有这个MS SQL存储过程:

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

但是我只收到了一些错误消息,就是

  

Msg 2627,Level 14,State 1,Procedure   Import_Agent_Client_Bucket_2010,Line   71违反PRIMARY KEY约束   'Client_Bucket_Client_PK'。不能   在对象中插入重复键   'dbo.Client_Bucket_Client'。该   声明已被终止。

1 个答案:

答案 0 :(得分:0)

修改

好的,我看到你在那里做什么,我为错过OUTPUT声明而道歉。基于该信息,如果在SELECT @Client_GUID = myNewPK FROM @myNewPKTable之前的行中没有将记录插入到Client表中,则代码可能会中断。如果没有插入记录,你最终会从前一条记录中获取GUID,当你去插入它时会导致PK违规。您可能必须检查以确保将记录插入到Client表中。

原始答案:

看起来你正在宣布一张桌子:

DECLARE @myNewPKTable TABLE (myNewPK UNIQUEIDENTIFIER)

但是你从来没有把任何东西放进去,所以这个语句必须返回null:

SELECT @Client_GUID = myNewPK FROM @myNewPKTable

编辑:

为什么不这样做呢?我不明白为什么表@myNewPKTable甚至被创建。

SET @Client_GUID = NEWID()

编辑:

我认为您获得主键冲突的原因是因为@Client_Bucket_GUID为空。在程序开始时,有以下代码:

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

运行此代码后,@ Client_Bucket_GUID将始终为null。同样,如果你想从中获取任何信息,你必须将记录插入@cbPKTable。如果您正在尝试创建新的UNIQUEIDENTIFIER并将其存储在@Client_Bucket_GUID中,只需使用NEWID()函数。