我的任务是使用存储过程复制公司信息。我必须使用光标来完成我的任务,作为我的技术主管的订单。每当我运行SP时,我都会收到此错误。其他表格,例如我在之前的问题中提到的人,地址都已解决,现在剩下的只有电话和电话号码。电话链接表让我很头疼。
P / S:
以下是我的代码:
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
电话表:
Phon_PhoneID | Phon_Number
--------------------------
1 | 11111111
2 | 22222222
3 | 33333333
4 | 44444444
现在每当我执行这个存储过程时,我都会收到如下所示的错误。我已尝试将每个Phone
表格与其PhoneLink
表格进行交换,以便首先将电话插入公司,但我仍然会重复两次以下错误:
消息547,级别16,状态0,过程DuplicateCompanyInfo,第150行(假设)
INSERT语句与FOREIGN KEY约束冲突" FK_PhoneLink_PhoneId"。冲突发生在数据库" CRM",table" dbo.Phone",column' Phon_PhoneId'。
消息547,级别16,状态0,过程DuplicateCompanyInfo,第160行(假设)
INSERT语句与FOREIGN KEY约束冲突" FK_PhoneLink_PhoneId"。冲突发生在数据库" CRM",table" dbo.Phone",column' Phon_PhoneId'。
我知道这是一个非常直接的错误消息,但我无法修复它。我是SQL的新手。请帮忙!
答案 0 :(得分:1)
您可以通过此命令查看所有约束:
exec sp_helpconstraint 'mytable'
似乎有重复。