Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Stubborn FK doesn't want to play nicely during insert statement Expand / Collapse
Author
Message
Posted Friday, May 10, 2013 3:18 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 24, 2014 9:05 PM
Points: 387, Visits: 418
Stupid Troublesome Error Code:

The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK600". The conflict occurred in database "HTC", table "dbo.COMMENTS", column 'GUID'.    

FK Setup:
The FK600 is set up as:
FK Base Table COMMENTS
FK Columns RELATED_GUID
Primary/Unique Key Base Table COMMENTS
Primary/Unique Key Columns GUID

SQL

INSERT INTO [dbo].[COMMENTS]
(
[GUID]
, [TYPE_CODE]
, [SUB_TYPE]
, [CUSTOMER]
, [TEXT]
, [RELATED_GUID]
, [CREATE_DATE]
, [CREATE_USER]
)
SELECT
tc.[GUID]
, tc.[TYPE_CODE]
, tc.[SUB_TYPE]
, tc.[CUSTOMER]
, tc.[TEXT]
, tc.[RELATED_GUID]
, tc.[CREATE_DATE]
, tc.[CREATE_USER]
FROM
[dbo].[TEMP_COMMENT] AS tc

TEMP_COMMENT is setup identical to COMMENT with the same data types. As each comment is replaced in the application with an updated version, the newly insert record in via the application creates a new GUID and references the old RELATED_GUID so I can see a trail. Note A, Guid = 1, RELATED_GUID = 1, replaced by Note B with new GUID 2 and RELATED_GUID = 1

I cannot insert a RELATED_GUID with a NULL, so I build the TEMP_COMMENT - GUID with REPLACE( NEWID(), '-', '') and run an update statement to SET RELATED_GUID = GUID. I have tried order by, cast the text as VARCHAR(MAX) --current data type is TEXT --, kicking the computer, dumping water on the keyboard, and yelling at it. Nothing works. Any ideas?
Post #1451747
Posted Friday, May 10, 2013 3:31 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 24, 2014 9:05 PM
Points: 387, Visits: 418
It might help if I add some data for you to insert...

INSERT INTO [dbo].[TEMP_COMMENT]
(
[GUID]
, [TYPE_CODE]
, [SUB_TYPE_CODE]
, [CUSTOMER]
, [TEXT]
, [RELATED_GUID]
, [CREATE_DATE]
, [CREATE_USER]

)
VALUES
(
REPLACE(NEWID() , '-' , '')
, 'A4'
, '103'
, '40004'
, 'Testing THE AUTOMATED NOTE INsertiON'
, NULL
, '2013-05-10 19:54:43'
, 'HAI'
)

Followed by:
UPDATE TEMP_COMMENT SET RELATED_GUID = GUID

Post #1451753
Posted Friday, May 10, 2013 3:33 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 24, 2014 9:05 PM
Points: 387, Visits: 418
AND maybe it would help if I gave you a table to insert into...???

CREATE TABLE [dbo].[TEMP_COMMENT]
(
[GUID] [varchar](32) NULL
, [TYPE_CODE] [varchar](15) NULL
, [SUB_TYPE_CODE] [varchar](15) NULL
, [CUSTOMER] [varchar](32) NULL
, [TEXT] [VARCHAR](MAX) NULL
, [RELATED_GUID] [varchar](32) NULL
, [CREATE_DATE] [datetime] NULL
, [CREATE_USER] [varchar](15) NULL
)

Post #1451754
Posted Friday, May 10, 2013 3:40 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:21 PM
Points: 22,506, Visits: 30,219
SQL_Enthusiast (5/10/2013)
AND maybe it would help if I gave you a table to insert into...???

CREATE TABLE [dbo].[TEMP_COMMENT]
(
[GUID] [varchar](32) NULL
, [TYPE_CODE] [varchar](15) NULL
, [SUB_TYPE_CODE] [varchar](15) NULL
, [CUSTOMER] [varchar](32) NULL
, [TEXT] [VARCHAR](MAX) NULL
, [RELATED_GUID] [varchar](32) NULL
, [CREATE_DATE] [datetime] NULL
, [CREATE_USER] [varchar](15) NULL
)



How about the other table and the constraints (FK) involved as well? Oh, some sample data for that table as well would help.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1451757
Posted Friday, May 10, 2013 3:52 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 24, 2014 9:05 PM
Points: 387, Visits: 418
Sorry Lynn

My COMMENT table (original table in DB) and TEMP_COMMENT are indentical in fields and data types. COMMENT has the FK on it while TEMP_COMMENT has no FK's or PK at all. It's built from an SSIS package that uses data from another system, transforms it, and places it TEMP_COMMENT where it waits for execution to be pushed into COMMENT...

I have attached a spreadsheet that has test data from my system, but in reality, it's identical to what TEMP_COMMENT will have in it after the SSIS packs loads it.


  Post Attachments 
Book2.xlsx (4 views, 9.54 KB)
Post #1451761
Posted Friday, May 10, 2013 3:56 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:21 PM
Points: 22,506, Visits: 30,219
SQL_Enthusiast (5/10/2013)
Sorry Lynn

My COMMENT table (original table in DB) and TEMP_COMMENT are indentical in fields and data types. COMMENT has the FK on it while TEMP_COMMENT has no FK's or PK at all. It's built from an SSIS package that uses data from another system, transforms it, and places it TEMP_COMMENT where it waits for execution to be pushed into COMMENT...

I have attached a spreadsheet that has test data from my system, but in reality, it's identical to what TEMP_COMMENT will have in it after the SSIS packs loads it.


I don't see any foreign keys defined on TEMP_COMMENT.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1451764
Posted Friday, May 10, 2013 3:57 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:21 PM
Points: 22,506, Visits: 30,219
SQL_Enthusiast (5/10/2013)
Sorry Lynn

My COMMENT table (original table in DB) and TEMP_COMMENT are indentical in fields and data types. COMMENT has the FK on it while TEMP_COMMENT has no FK's or PK at all. It's built from an SSIS package that uses data from another system, transforms it, and places it TEMP_COMMENT where it waits for execution to be pushed into COMMENT...

I have attached a spreadsheet that has test data from my system, but in reality, it's identical to what TEMP_COMMENT will have in it after the SSIS packs loads it.


Also, your FK error is on dbo.Comments (per your original post), not on TEMP_COMMENT.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1451765
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse