我的任務是使用存儲過程複製公司信息。我必須使用光標來完成我的任務,作爲我的技術主管的訂單。每當我運行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新手。請幫忙!
請閱讀如何創建[mcve]。您發佈了很多代碼,請僅關注與問題相關的代碼。 –
對不起,先生,我已經通過刪除'Person_Phone'來編輯和縮短它,只留下'公司電話',是否可以? –
我不是在問你,我是在問你。你的問題將有更好的機會得到更快,更準確的答案,越短越清晰。例如,我通常甚至不願意閱讀與您的問題一樣長的問題,我相信我不是唯一的問題。請記住,我們都在業餘時間,沒有人能夠通過計算器來回答問題。幫助我們儘可能簡短,專注和可讀地幫助您解決問題。 –