Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SQL Primary Key Issue

SQL Primary Key Issue

Scheduled Pinned Locked Moved C#
helpdatabasesales
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Glen Childs
    wrote on last edited by
    #1

    Hello,

    I have imported a CSV file into SQL, the wizard has added this as a new table.

    I am trying to run a SQL Statement to insert the data from the imported table into the one I need to use in the new data table.

    However I am getting the following error.

    Collapse | Copy Code
    Msg 2627, Level 14, State 1, Line 4
    Violation of PRIMARY KEY constraint 'PK_Customer'. Cannot insert duplicate key in object 'dbo.Customer'. The duplicate key value is (105).
    The statement has been terminated.

    This is my statement:

    Collapse | Copy Code
    INSERT INTO dbo.Customer
    (custID,lastName,firstName,title,address1,address2,address3,postCode,email,fax)
    SELECT ACCOUNT,SURNAME,FORENAMES,TITLE,ADD1,ADD2,ADD3,POSTCODE,EMAIL,FAX
    FROM dbo.[NCCUST CSV FORMAT PFS 9.1.14];

    Any pointers would be great Smile | :)

    Thanks

    W S W 3 Replies Last reply
    0
    • G Glen Childs

      Hello,

      I have imported a CSV file into SQL, the wizard has added this as a new table.

      I am trying to run a SQL Statement to insert the data from the imported table into the one I need to use in the new data table.

      However I am getting the following error.

      Collapse | Copy Code
      Msg 2627, Level 14, State 1, Line 4
      Violation of PRIMARY KEY constraint 'PK_Customer'. Cannot insert duplicate key in object 'dbo.Customer'. The duplicate key value is (105).
      The statement has been terminated.

      This is my statement:

      Collapse | Copy Code
      INSERT INTO dbo.Customer
      (custID,lastName,firstName,title,address1,address2,address3,postCode,email,fax)
      SELECT ACCOUNT,SURNAME,FORENAMES,TITLE,ADD1,ADD2,ADD3,POSTCODE,EMAIL,FAX
      FROM dbo.[NCCUST CSV FORMAT PFS 9.1.14];

      Any pointers would be great Smile | :)

      Thanks

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      Well, the error says it all. There are obviously 2 customers with the value 105 in the ACCOUNT field in the 'CSV' table. You need to either remove the Primary key restraint on dbo.customer (not recommended) or remove one of the customers in the 'CSV' table with ACCOUNT 105.

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

      1 Reply Last reply
      0
      • G Glen Childs

        Hello,

        I have imported a CSV file into SQL, the wizard has added this as a new table.

        I am trying to run a SQL Statement to insert the data from the imported table into the one I need to use in the new data table.

        However I am getting the following error.

        Collapse | Copy Code
        Msg 2627, Level 14, State 1, Line 4
        Violation of PRIMARY KEY constraint 'PK_Customer'. Cannot insert duplicate key in object 'dbo.Customer'. The duplicate key value is (105).
        The statement has been terminated.

        This is my statement:

        Collapse | Copy Code
        INSERT INTO dbo.Customer
        (custID,lastName,firstName,title,address1,address2,address3,postCode,email,fax)
        SELECT ACCOUNT,SURNAME,FORENAMES,TITLE,ADD1,ADD2,ADD3,POSTCODE,EMAIL,FAX
        FROM dbo.[NCCUST CSV FORMAT PFS 9.1.14];

        Any pointers would be great Smile | :)

        Thanks

        S Offline
        S Offline
        Simon_Whale
        wrote on last edited by
        #3

        I know that you have had a great answer from Wayne, but you would better posting SQL questions next time with the correct forum Database[^]

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        1 Reply Last reply
        0
        • G Glen Childs

          Hello,

          I have imported a CSV file into SQL, the wizard has added this as a new table.

          I am trying to run a SQL Statement to insert the data from the imported table into the one I need to use in the new data table.

          However I am getting the following error.

          Collapse | Copy Code
          Msg 2627, Level 14, State 1, Line 4
          Violation of PRIMARY KEY constraint 'PK_Customer'. Cannot insert duplicate key in object 'dbo.Customer'. The duplicate key value is (105).
          The statement has been terminated.

          This is my statement:

          Collapse | Copy Code
          INSERT INTO dbo.Customer
          (custID,lastName,firstName,title,address1,address2,address3,postCode,email,fax)
          SELECT ACCOUNT,SURNAME,FORENAMES,TITLE,ADD1,ADD2,ADD3,POSTCODE,EMAIL,FAX
          FROM dbo.[NCCUST CSV FORMAT PFS 9.1.14];

          Any pointers would be great Smile | :)

          Thanks

          W Offline
          W Offline
          webhosting957
          wrote on last edited by
          #4

          Well, the error says it all. There are obviously 2 customers with the value 105 in the ACCOUNT field in the 'CSV' table. You need to either remove the Primary key restraint on dbo.customer (not recommended) or remove one of the customers in the 'CSV' table with ACCOUNT 105.
          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

          Hosting Charges || Domain Registrars

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups