Results 1 to 5 of 5

Thread: Why do I copy Table1 contents to Table with the error ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    448

    Why do I copy Table1 contents to Table with the error ?

    I have 2 Tables: TABQUANHEGD: destination and TABNGUON: source
    I implemented sql in SQL Server 2005 with an error

    Code:
      
    INSERT INTO TABQUANHEGD(IDQHGD, MASTT, NGUOITHAN, NGAYSINH, MAGT, MAQH, MAQT, OHIENTAI, NGHENGHIEP, NOILAM, DIENTHOAI, TRUGIACANH, NGAYBD, NGAYKT, LIENHE, DAMAT, ANH, GHICHU)
    SELECT IDQHGD, MASTT, NGUOITHAN, NGAYSINH, MAGT, MAQH, MAQT, OHIENTAI, NGHENGHIEP, NOILAM, DIENTHOAI, TRUGIACANH, NGAYBD, NGAYKT, LIENHE, DAMAT, ANH, GHICHU FROM TABNGUON;
    Msg 547, Level 16, State 0, Line 1
    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TABKHOKIEMKECT_TABKHOKIEMKE". The conflict occurred in database "NhanSu", table "dbo.TABQUANHEGDTT", column 'IDQH'.
    The statement has been terminated.



    In the database I don't have these 2 tables:
    TABKHOKIEMKECT
    TABKHOKIEMKE
    I don't know where to get these 2 Tables to report an error
    I want to copy the content from Table1 to Table2, how do I write an SQL query ?

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,428

    Re: Why do I copy Table1 contents to Table with the error ?

    What is the definition of the table TABQUANHEGD I will be that there is a foreign key named FK_TABKHOKIEMKECT_TABKHOKIEMKE on the table that references table TABQUANHEGDTT. If you do not want the key in place drop it.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    448

    Re: Why do I copy Table1 contents to Table with the error ?

    In my data there are no these 2 Tables: "FK_TABKHOKIEMKECT_TABKHOKIEMKE" and I have not created a relationship to the Table TABQUANHEGDTT, how to fix this? you see my database attachment.
    http://www.mediafire.com/file/4a4fj4...seErr.rar/file

  4. #4
    Lively Member
    Join Date
    Jan 2020
    Posts
    100

    Re: Why do I copy Table1 contents to Table with the error ?

    it's worked good for me
    plz try this :

    HTML Code:
    INSERT INTO DB2.TB1  SELECT * from DB1.TB1;

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,588

    Re: Why do I copy Table1 contents to Table with the error ?

    Quote Originally Posted by dong View Post
    In my data there are no these 2 Tables: "FK_TABKHOKIEMKECT_TABKHOKIEMKE" and I have not created a relationship to the Table TABQUANHEGDTT
    The error message is telling you that the destination has a foreign key with that name and you are not honouring it. The fix is either to get rid of that foreign key or to honour it. It sounds like you don't want it so you should probably get rid of it. Stop talking about what tables you do and don't have because that's irrelevant. Look for the foreign key with that name as you should have done the moment you saw that error message and also when GaryMazzone told you to. While foreign keys are generally named after the tables they relate, they don't have to be so that you don't have tables that match the foreign key name is irrelevant.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width