2016-04-25 98 views
0

我的任務是使用存儲過程複製公司信息。我必須使用光標來完成我的任務,作爲我的技術主管的訂單。每當我運行SP時,我都會遇到這個錯誤。其他表格,如我在前面的問題中提到的人員,地址已經解決,現在剩下的只是電話&電話鏈接表,這讓我很頭疼。INSERT語句與FOREIGN KEY約束衝突(光標)

P/S:

  • ENTITYID = 5(公司)

下面是我的代碼:

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[DuplicateCompanyInfo] 
    @Comp_CompanyId NVARCHAR(80) 


AS 
BEGIN 
    SET NOCOUNT ON; 

DECLARE @CompanyID NVARCHAR(30), 
     @PersonID NVARCHAR(30), 
     @AddressID NVARCHAR(30), 
     @PhoneLinkID NVARCHAR(30), 
     @PhoneID NVARCHAR(30) 
      EXEC @companyId = crm_next_id 5 
      EXEC @PersonId = crm_next_id 13 
      EXEC @AddressId = crm_next_id 1 



     -- Add Company 
     INSERT INTO Company 
     (
      Comp_CompanyId, Comp_PrimaryPersonId, Comp_PrimaryAddressId, Comp_Name, Comp_Type, Comp_Status, Comp_CreatedBy, 
      Comp_CreatedDate, Comp_UpdatedBy, Comp_UpdatedDate, Comp_TimeStamp, Comp_SecTerr, Comp_WebSite 
     ) 
     SELECT @companyId, @PersonId, @AddressId, Comp_Name, Comp_Type, Comp_Status, '1', 
       GETDATE(), '1', GETDATE(), GETDATE(), Comp_SecTerr, Comp_WebSite 

     FROM Company 
     WHERE Comp_CompanyId = @comp_companyid 
     AND Comp_Deleted is null 


------- Company PhoneLink (Business) Cursor ----------------------------------------- 
     -- Declare Variables 
     DECLARE @c_PLink_LinkID NVARCHAR(30) 
     DECLARE @c_PLink_PhoneId NVARCHAR(30) 
     DECLARE @c_PLink_CreatedBy NVARCHAR(30) 
     DECLARE @c_PLink_CreatedDate NVARCHAR(30) 
     DECLARE @c_PLink_UpdatedDate NVARCHAR(30) 
     DECLARE @c_PLink_TimeStamp NVARCHAR(30) 
     DECLARE @c_PLink_EntityID NVARCHAR(30) 
     DECLARE @c_PLink_RecordID NVARCHAR(30) 
     DECLARE @c_PLink_Type NVARCHAR(30) 


     --Declare Cursor 
     DECLARE CompPhoneLinkCursor CURSOR FOR 
     SELECT PLink_LinkID, PLink_PhoneId, PLink_CreatedBy, PLink_CreatedDate, PLink_UpdatedDate, PLink_TimeStamp, 
       PLink_EntityID, PLink_RecordID, PLink_Type 
     FROM PhoneLink 
     INNER JOIN Phone 
     ON PLink_PhoneId = Phon_PhoneId 
     AND PLink_RecordID = @Comp_CompanyId 
     AND Plink_EntityID = '5' 
     AND Plink_Type = 'Business' 
     AND Phon_Deleted is null 
     AND Plink_Deleted is null 


     --Open Cursor & fetch 1st row into variables 
     OPEN CompPhoneLinkCursor 
     FETCH NEXT FROM CompPhoneLinkCursor INTO @c_PLink_LinkID, @c_PLink_PhoneId, @c_PLink_CreatedBy, @c_PLink_CreatedDate, 
             @c_PLink_UpdatedDate, @c_PLink_TimeStamp, @c_PLink_EntityID, @c_PLink_RecordID, @c_PLink_Type 


     --Fetch successful 
     --Check for a new row 
     WHILE @@FETCH_STATUS = 0 
     BEGIN 

     EXEC @PhoneLinkID = crm_next_id 10208 
     EXEC @PhoneId = crm_next_id 14 


      INSERT INTO PhoneLink 
      (
       PLink_LinkID, PLink_PhoneId, PLink_CreatedBy, PLink_CreatedDate, PLink_UpdatedDate, 
       PLink_TimeStamp, PLink_EntityID, PLink_RecordID, PLink_Type 
      ) 
      VALUES 
      (
       @PhoneLinkID, @PhoneId, '1', GETDATE(), GETDATE(), 
       GETDATE(), @c_PLink_EntityID, @c_PLink_RecordID, 'Business' 
      ) 


     --Get next available row into variables 
     FETCH NEXT FROM CompPhoneLinkCursor INTO @c_PLink_LinkID, @c_PLink_PhoneID, @c_PLink_CreatedBy, @c_PLink_CreatedDate, 
             @c_PLink_UpdatedDate, @c_PLink_TimeStamp, @c_PLink_EntityID, @c_PLink_RecordID, @c_PLink_Type 

     END 

     CLOSE CompPhoneLinkCursor 
     DEALLOCATE CompPhoneLinkCursor 



------- Company Phone (Business) Cursor --------------------------------------------- 
     -- Declare Variables 
     DECLARE @c_Phon_PhoneId NVARCHAR(30) 
     DECLARE @c_Phon_Number NVARCHAR(30) 
     DECLARE @c_Phon_CreatedBy NVARCHAR(30) 
     DECLARE @c_Phon_CreatedDate NVARCHAR(30) 
     DECLARE @c_Phon_UpdatedBy NVARCHAR(30) 
     DECLARE @c_Phon_UpdatedDate NVARCHAR(30) 
     DECLARE @c_Phon_TimeStamp NVARCHAR(30) 


     --Declare Cursor 
     DECLARE CompPhoneCursor CURSOR FOR 
     SELECT Phon_PhoneId, Phon_Number, Phon_CreatedBy, Phon_CreatedDate, Phon_UpdatedBy, Phon_UpdatedDate, Phon_TimeStamp 
     FROM Phone 
     INNER JOIN PhoneLink 
     ON Phon_PhoneId = PLink_PhoneId 
     AND PLink_RecordID = @Comp_CompanyId 
     AND Plink_EntityID = '5' 
     AND Plink_Type = 'Business' 
     AND Phon_Deleted is null 
     AND Plink_Deleted is null 


     --Open Cursor & fetch 1st row into variables 
     OPEN CompPhoneCursor 
     FETCH NEXT FROM CompPhoneCursor INTO @c_Phon_PhoneId, @c_Phon_Number, @c_Phon_CreatedBy, @c_Phon_CreatedDate, 
             @c_Phon_UpdatedBy, @c_Phon_UpdatedDate, @c_Phon_TimeStamp 


     --Fetch successful 
     --Check for a new row 
     WHILE @@FETCH_STATUS = 0 
     BEGIN 


     INSERT INTO Phone 
     (
      Phon_PhoneId, Phon_Number, Phon_CreatedBy, Phon_CreatedDate, Phon_UpdatedBy, Phon_UpdatedDate, Phon_TimeStamp 
     ) 
     VALUES 
     (
      @PhoneID, @c_Phon_Number, '1', GETDATE(), '1', GETDATE(), GETDATE() 
     ) 


     --Get next available row into variables 
     FETCH NEXT FROM CompPhoneCursor INTO @c_Phon_PhoneId, @c_Phon_Number, @c_Phon_CreatedBy, @c_Phon_CreatedDate, 
             @c_Phon_UpdatedBy, @c_Phon_UpdatedDate, @c_Phon_TimeStamp 

     END 

     CLOSE CompPhoneCursor 
     DEALLOCATE CompPhoneCursor 



------- Company PhoneLink (Fax) Cursor ---------------------------------------------- 
     -- Declare Variables 
     DECLARE @cf_PLink_LinkID NVARCHAR(30) 
     DECLARE @cf_PLink_PhoneId NVARCHAR(30) 
     DECLARE @cf_PLink_CreatedBy NVARCHAR(30) 
     DECLARE @cf_PLink_CreatedDate NVARCHAR(30) 
     DECLARE @cf_PLink_UpdatedDate NVARCHAR(30) 
     DECLARE @cf_PLink_TimeStamp NVARCHAR(30) 
     DECLARE @cf_PLink_EntityID NVARCHAR(30) 
     DECLARE @cf_PLink_RecordID NVARCHAR(30) 
     DECLARE @cf_PLink_Type NVARCHAR(30) 


     --Declare Cursor 
     DECLARE CompanyFaxLinkCursor CURSOR FOR 
     SELECT PLink_LinkID, PLink_PhoneId, PLink_CreatedBy, PLink_CreatedDate, PLink_UpdatedDate, PLink_TimeStamp, 
       PLink_EntityID, PLink_RecordID, PLink_Type 
     FROM PhoneLink 
     INNER JOIN Phone 
     ON PLink_PhoneId = Phon_PhoneId 
     AND PLink_RecordID = @Comp_CompanyId 
     AND Plink_EntityID = '5' 
     AND Plink_Type = 'Fax' 
     AND Plink_Deleted is null 
     AND Phon_Deleted is null 


     --Open Cursor & fetch 1st row into variables 
     OPEN CompanyFaxLinkCursor 
     FETCH NEXT FROM CompanyFaxLinkCursor INTO @cf_PLink_LinkID, @cf_PLink_PhoneId, @cf_PLink_CreatedBy, @cf_PLink_CreatedDate, 
             @cf_PLink_UpdatedDate, @cf_PLink_TimeStamp, @cf_PLink_EntityID, @cf_PLink_RecordID, @cf_PLink_Type 


     --Fetch successful 
     --Check for a new row 
     WHILE @@FETCH_STATUS = 0 
     BEGIN 

     EXEC @PhoneLinkID = crm_next_id 10208 
     EXEC @PhoneId = crm_next_id 14 


      INSERT INTO PhoneLink 
      (
       PLink_LinkID, PLink_PhoneId, PLink_CreatedBy, PLink_CreatedDate, PLink_UpdatedDate, 
       PLink_TimeStamp, PLink_EntityID, PLink_RecordID, PLink_Type 
      ) 
      VALUES 
      (
       @PhoneLinkID, @PhoneId, '1', GETDATE(), GETDATE(), 
       GETDATE(), @cf_PLink_EntityID, @cf_PLink_RecordID, 'Fax' 
      )   


     --Get next available row into variables 
     FETCH NEXT FROM CompanyFaxLinkCursor INTO @cf_PLink_LinkID, @cf_PLink_PhoneID, @cf_PLink_CreatedBy, @cf_PLink_CreatedDate, 
             @cf_PLink_UpdatedDate, @cf_PLink_TimeStamp, @cf_PLink_EntityID, @cf_PLink_RecordID, @cf_PLink_Type 

     END 

     CLOSE CompanyFaxLinkCursor 
     DEALLOCATE CompanyFaxLinkCursor 



------- Company Phone (Fax) Cursor -------------------------------------------------- 
     -- Declare Variables 
     DECLARE @cf_Phon_PhoneId NVARCHAR(30) 
     DECLARE @cf_Phon_Number NVARCHAR(30) 
     DECLARE @cf_Phon_CreatedBy NVARCHAR(30) 
     DECLARE @cf_Phon_CreatedDate NVARCHAR(30) 
     DECLARE @cf_Phon_UpdatedBy NVARCHAR(30) 
     DECLARE @cf_Phon_UpdatedDate NVARCHAR(30) 
     DECLARE @cf_Phon_TimeStamp NVARCHAR(30) 


     --Declare Cursor 
     DECLARE CompanyFaxCursor CURSOR FOR 
     SELECT Phon_PhoneId, Phon_Number, Phon_CreatedBy, Phon_CreatedDate, Phon_UpdatedBy, Phon_UpdatedDate, Phon_TimeStamp 
     FROM Phone 
     INNER JOIN PhoneLink 
     ON Phon_PhoneId = PLink_PhoneId 
     AND PLink_RecordID = @Comp_CompanyId 
     AND Plink_EntityID = '5' 
     AND Plink_Type = 'Fax' 
     AND Plink_Deleted is null 
     AND Phon_Deleted is null 


     --Open Cursor & fetch 1st row into variables 
     OPEN CompanyFaxCursor 
     FETCH NEXT FROM CompanyFaxCursor INTO @cf_Phon_PhoneId, @cf_Phon_Number, @cf_Phon_CreatedBy, @cf_Phon_CreatedDate, 
             @cf_Phon_UpdatedBy, @cf_Phon_UpdatedDate, @cf_Phon_TimeStamp 


     --Fetch successful 
     --Check for a new row 
     WHILE @@FETCH_STATUS = 0 
     BEGIN 


     INSERT INTO Phone 
     (
      Phon_PhoneId, Phon_Number, Phon_CreatedBy, Phon_CreatedDate, Phon_UpdatedBy, Phon_UpdatedDate, Phon_TimeStamp 
     ) 
     VALUES 
     (
      @PhoneID, @cf_Phon_Number, '1', GETDATE(), '1', GETDATE(), GETDATE() 
     ) 


     --Get next available row into variables 
     FETCH NEXT FROM CompanyFaxCursor INTO @cf_Phon_PhoneId, @cf_Phon_Number, @cf_Phon_CreatedBy, @cf_Phon_CreatedDate, 
             @cf_Phon_UpdatedBy, @cf_Phon_UpdatedDate, @cf_Phon_TimeStamp 

     END 

     CLOSE CompanyFaxCursor 
     DEALLOCATE CompanyFaxCursor 

我的表結構如下:

電話鏈接表:

PLink_LinkID | PLink_EntityID | PLink_RecordID | PLink_Type | PLink_PhoneID 
------------------------------------------------------------------------- 
1   | 5    | 2    | Business | 1 
2   | 5    | 3    | Fax  | 2 
3   | 5    | 2    | Fax  | 3 
4   | 5    | 3    | Business | 4 

電話表:

​​3210

現在,每當我執行我會得到下圖所示的錯誤此存儲過程。我試圖以先插入手機只與公司的鏈接交換每Phone表以其PhoneLink表,但我還是得到了以下錯誤重複兩次:

消息547,級別16,狀態0,過程DuplicateCompanyInfo ,行150(假設)

INSERT語句與FOREIGN KEY約束「FK_PhoneLink_PhoneId」衝突。衝突發生在數據庫「CRM」中,表「dbo.Phone」,列'Phon_PhoneId'。

消息547,級別16,狀態0,過程DuplicateCompanyInfo,160線(假定)

INSERT語句衝突與外鍵約束 「FK_PhoneLink_PhoneId」。衝突發生在數據庫「CRM」中,表「dbo.Phone」,列'Phon_PhoneId'。

我知道這是一個非常直接的錯誤消息,但我無法修復它。我是SQL新手。請幫忙!

+1

請閱讀如何創建[mcve]。您發佈了很多代碼,請僅關注與問題相關的代碼。 –

+0

對不起,先生,我已經通過刪除'Person_Phone'來編輯和縮短它,只留下'公司電話',是否可以? –

+1

我不是在問你,我是在問你。你的問題將有更好的機會得到更快,更準確的答案,越短越清晰。例如,我通常甚至不願意閱讀與您的問題一樣長的問題,我相信我不是唯一的問題。請記住,我們都在業餘時間,沒有人能夠通過計算器來回答問題。幫助我們儘可能簡短,專注和可讀地幫助您解決問題。 –

回答

0

您可以查看該命令所有的約束:

exec sp_helpconstraint 'mytable' 

好像有重複。

+0

我檢查了PhoneLink表,它確實有外鍵約束,約束名稱爲FK_PhoneLink_PhoneID,status_enable爲「enabled」,status_for_replication爲「Is_For_Replication」,constraint_keys爲「PLink_PhoneID」和REFERENCES CRM.dbo.Phone Phon_PhoneId)。問題是我不知道如何解決這個約束問題。我是SQL新手,可能會提出很容易解決的問題,併爲此表示歉意。 –

+0

我已經upvoted&接受你的答案,因爲我相信你已經幫我清除了一些困惑,但仍然希望你能指導我解決約束問題。謝謝 –

+0

我已經通過將'VALUES'中的所有'@c_PLink_EntityID,@ c_PLink_RecordID'更改爲''5',@ companyid'來解決它,並且它現在完美運行。不管怎麼說,還是要謝謝你! –

相關問題