sql - The INSERT statement conflicted with the FOREIGN KEY constraint (Cursor) -


my task duplicate company information using stored procedure. have use cursor complete task order tech lead. got error whenever run sp. other tables such person, address have mentioned in previous questions solved, what's left phone & phone link table giving me headache.

p/s:

  • entityid = 5 (company)

below code:

set ansi_nulls on go set quoted_identifier on go  alter procedure [dbo].[duplicatecompanyinfo]     @comp_companyid nvarchar(80)   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 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          company         comp_companyid = @comp_companyid         , comp_deleted 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         select plink_linkid, plink_phoneid, plink_createdby, plink_createddate, plink_updateddate, plink_timestamp,                plink_entityid, plink_recordid, plink_type         phonelink         inner join phone         on plink_phoneid = phon_phoneid         , plink_recordid = @comp_companyid         , plink_entityid = '5'         , plink_type = 'business'         , phon_deleted null         , plink_deleted null           --open cursor & fetch 1st row variables         open compphonelinkcursor         fetch next compphonelinkcursor @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 new row         while @@fetch_status = 0         begin          exec @phonelinkid = crm_next_id 10208         exec @phoneid = crm_next_id 14               insert 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 variables         fetch next compphonelinkcursor @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         select phon_phoneid, phon_number, phon_createdby, phon_createddate, phon_updatedby, phon_updateddate, phon_timestamp         phone         inner join phonelink         on phon_phoneid = plink_phoneid         , plink_recordid = @comp_companyid         , plink_entityid = '5'         , plink_type = 'business'         , phon_deleted null         , plink_deleted null           --open cursor & fetch 1st row variables         open compphonecursor         fetch next compphonecursor @c_phon_phoneid, @c_phon_number, @c_phon_createdby, @c_phon_createddate,                                       @c_phon_updatedby, @c_phon_updateddate, @c_phon_timestamp           --fetch successful         --check new row         while @@fetch_status = 0         begin           insert 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 variables         fetch next compphonecursor @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         select plink_linkid, plink_phoneid, plink_createdby, plink_createddate, plink_updateddate, plink_timestamp,                plink_entityid, plink_recordid, plink_type         phonelink         inner join phone         on plink_phoneid = phon_phoneid         , plink_recordid = @comp_companyid         , plink_entityid = '5'         , plink_type = 'fax'         , plink_deleted null         , phon_deleted null           --open cursor & fetch 1st row variables         open companyfaxlinkcursor         fetch next companyfaxlinkcursor @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 new row         while @@fetch_status = 0         begin          exec @phonelinkid = crm_next_id 10208         exec @phoneid = crm_next_id 14               insert 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 variables         fetch next companyfaxlinkcursor @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         select phon_phoneid, phon_number, phon_createdby, phon_createddate, phon_updatedby, phon_updateddate, phon_timestamp         phone         inner join phonelink         on phon_phoneid = plink_phoneid         , plink_recordid = @comp_companyid         , plink_entityid = '5'         , plink_type = 'fax'         , plink_deleted null         , phon_deleted null           --open cursor & fetch 1st row variables         open companyfaxcursor         fetch next companyfaxcursor @cf_phon_phoneid, @cf_phon_number, @cf_phon_createdby, @cf_phon_createddate,                                       @cf_phon_updatedby, @cf_phon_updateddate, @cf_phon_timestamp           --fetch successful         --check new row         while @@fetch_status = 0         begin           insert 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 variables         fetch next companyfaxcursor @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 

my table structure below:

phone link table:

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 

phone table:

 phon_phoneid | phon_number  --------------------------  1            | 11111111  2            | 22222222  3            | 33333333  4            | 44444444 

now whenever execute stored procedure error shown below. i've tried swapping every phone table phonelink table in order insert phone first link company, still got following error repeated twice:

msg 547, level 16, state 0, procedure duplicatecompanyinfo, line 150 (assumed)

the insert statement conflicted foreign key constraint "fk_phonelink_phoneid". conflict occurred in database "crm", table "dbo.phone", column 'phon_phoneid'.

msg 547, level 16, state 0, procedure duplicatecompanyinfo, line 160 (assumed)

the insert statement conflicted foreign key constraint "fk_phonelink_phoneid". conflict occurred in database "crm", table "dbo.phone", column 'phon_phoneid'.

i know it's straight forward error message can't fix it. i'm new in sql. please help!

you can view constraints command:

exec sp_helpconstraint 'mytable' 

seems there duplicates.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -