Solved

SQL 2005 - Insert Into (Foreign Key Issue).

Posted on 2009-06-30
10
567 Views
Last Modified: 2012-05-07
I think I have a Foreign Key issue and need some help on my insert.

Error Message recvd in application:

IDispatch error #3119
Number=547  State=23000  Source=Microsoft SQL Native Client
Description=The INSERT statement conflicted with the FOREIGN KEY constraint "FK_SC_ClearinghouseId". The conflict occurred in database "DemoCPS", table "dbo.Clearinghouse", column 'ClearinghouseId'.
Number=3621  State=01000  Source=Microsoft SQL Native Client
Description=The statement has been terminated.
Number=0  State=01000  Source=Microsoft SQL Native Client
Description=Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

INSERT  INTO dbo.Settings
        ( ClearinghouseId ,
          InsuranceCarrierCompanyId ,
          InsuranceCarrierCompanyProcessingId ,
          ECSInsuranceCarriersId ,
          PlugInId ,
          Type ,
          Name ,
          Value ,
          Created ,
          CreatedBy ,
          LastModified ,
          LastModifiedBy
        )
VALUES  ( 18 , -- ClearinghouseId - int
          NULL , -- InsuranceCarrierCompanyId - int
          NULL , -- InsuranceCarrierCompanyProcessingId - int
          NULL , -- ECSInsuranceCarriersId - int
          NULL , -- PlugInId - int
          'ClearinghouseTransmission' , -- Type - varchar(50)
          'AccountNumber' , -- Name - varchar(50)
          '13562' , -- Value - varchar(255)
          '2009-06-30 20:36:02' , -- Created - datetime
          'Jeff' , -- CreatedBy - varchar(30)
          '2009-06-30 20:36:02' , -- LastModified - datetime
          'Jeff'  -- LastModifiedBy - varchar(30)
        )

Open in new window

0
Comment
Question by:Jeff S
10 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 50 total points
ID: 24749545
this is because, you are trying to insert a value into the column whose value does not exists on 'ClearinghouseId' column
0
 
LVL 7

Author Comment

by:Jeff S
ID: 24749574
which value? I have a ClearinghouseId with that Id number.
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 50 total points
ID: 24749634
Please confirm you have the value 18 in whatever table FK_SC_ClearingHouseID is referenced from?
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 7

Author Comment

by:Jeff S
ID: 24749936
Yes, 18 exists. See attached screen shot.
6-30-2009-4-43-19-PM.pdf
0
 

Assisted Solution

by:turbohappy
turbohappy earned 50 total points
ID: 24750032
Can you get the text of the constraint FK_SC_ClearinghouseId ? Could be pointed to the wrong column on Settings or something silly like that.
0
 
LVL 42

Accepted Solution

by:
EugeneZ earned 100 total points
ID: 24751259
you can try to disable  CONSTRAINTs

for example

ALTER TABLE Settings
 NOCHECK CONSTRAINT ALL

insert into Settings



ALTER TABLE Settings
 CHECK CONSTRAINT ALL


see more
Microsoft SQL Server Constraints
http://www.odetocode.com/Articles/79.aspx


0
 
LVL 23

Assisted Solution

by:Racim BOUDJAKDJI
Racim BOUDJAKDJI earned 100 total points
ID: 24752354
The error message seems to indicate that the broken constraint concerns the "dbo.Clearinghouse" table in column 'ClearinghouseId' not in Settings.   It seems the INSERT fails because there  is no parent key in the Clearinghouse table.  

Have you tried running

select * from dbo.Clearinghouse where ClearinghouseId = 18
0
 
LVL 14

Assisted Solution

by:Jagdish Devaku
Jagdish Devaku earned 100 total points
ID: 24752492
hi,
'ClearinghouseId' in 'Settings' tables is referred to 'ClearinghouseId' in 'Clearinghouse' table.
before inserting any value for 'ClearinghouseId'  in 'Settings' make sure that the value has an entry in 'ClearinghouseId'  in 'ClearingHouse' table.
if you dont have the value in 'ClearingHouse' table and trying to insert in 'Settings' table then only you will receive the above error.
so check it and fo the necessary.
bye.
0
 
LVL 2

Assisted Solution

by:kbac
kbac earned 50 total points
ID: 24753778
Can you check if you have the 18 in Settings table and if you have unique index checked in this constraint? You may have disabled multiple insertions.
0
 
LVL 7

Author Closing Comment

by:Jeff S
ID: 31598535
Split points to be fair.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question