SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stubborn FK doesn't want to play nicely during insert statement


Stubborn FK doesn't want to play nicely during insert statement

Author
Message
SQL_Enthusiast
SQL_Enthusiast
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 422
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?
SQL_Enthusiast
SQL_Enthusiast
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 422
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


SQL_Enthusiast
SQL_Enthusiast
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 422
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
)


Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39642 Visits: 38560
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.

Cool
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)
SQL_Enthusiast
SQL_Enthusiast
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 422
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.
Attachments
Book2.xlsx (8 views, 9.00 KB)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39642 Visits: 38560
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.

Cool
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)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39642 Visits: 38560
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.

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search