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

  • 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?

  • 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

  • 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

    )

  • 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.

  • 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.

  • 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.

  • 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.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply