BCP import then export

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rockmead76
    New Member
    • Feb 2008
    • 2

    BCP import then export

    Hi,

    I'm not a SQL person but have the need to export a tables content (from live) and then import into a test system. A backup and restore is not an option.

    bcp Database..datab asetable out "c:\Documen ts and Settings\Admini strator\My Documents\test3 .txt" -c -P

    Exports ok (I would like to export the tables column headers aswell), but if I try to reimport this file on the same server using

    bcp Database..datab asetable in c:\Documents and Settings\Admini strator\My Documents\test3 .txt" -S(local) -c -P password

    but I get the following error.....


    Starting copy...
    SQLState = 23000, NativeError = 2627
    Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY
    constraint 'PK___2__10'. Cannot insert duplicate key in object 'ComputerEntry' .
    SQLState = 01000, NativeError = 3621
    Warning = [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been
    terminated.

    BCP copy in failed

    C:\Documents and Settings\Admini strator\My Documents>Pause
    Press any key to continue . . .

    Why does export and import fail on the same server (I'm testing on my test system)

    I'm afraid this error means little and the more I search google the more confused I get !!

    Thanks inadvance

    Paul
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    The error message suggests you are importing data to a table and by this process making some primary key violation. ie--trying to insert a duplicate value into the table.

    Comment

    • radcaesar
      Recognized Expert Contributor
      • Sep 2006
      • 759

      #3
      Hey, You had exported the values first time and the second time you are trying to insert duplicate values and that violates key.

      Truncate the table and then try...

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        The rows that you just exported is still on the table. When you BCP-in, it will not delete those existing rows and just add the ones you just imported. Which makes everything a duplicate. Since the table has a duplicate primary key constraint/check, maybe through an index, it gives you the error.

        -- CK

        Comment

        Working...