Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Unanswered: How to fix Violation of PRIMARY KEY

    Dears

    Plz any one help me

    I got these errors

    ==========
    Microsoft OLE DB Provider for SQL Server error '80040e2f'

    Violation of PRIMARY KEY constraint 'PK_STB_MST'. Cannot insert
    duplicate key in object 'STB_MST'.

    /Mgmt_mst/shell2star_proc.asp, line 224
    =============

    my Code

    '================================================= ========================
    ' server start
    '================================================= ========================
    strSQL = "DELETE stb_mst "
    objDBStar.execute StrSQL
    '================================================= ========================
    strSQL = "SELECT * FROM server"

    Set objRs= Server.CreateObject("ADODB.RecordSet")
    With objRs
    .CursorLocation = 3
    .CursorType = 0
    .LockType = 1
    .ActiveConnection = objDBShell
    .Open StrSQL
    End With

    Do While Not objRs.eof
    strSQL = "INSERT INTO stb_mst( " &_
    " STB_ID, IP, MAC, BUILDING, AISLE, "&_
    " CELL_NO, ROOM_A, STB_VERSION, STATUS, CH_GROUP, "&_
    " STB_ONOFF, TV_ONOFF, PX, PY, PW, "&_
    " PH, REG_DATE, ENABLE "&_
    " )" & _
    " VALUES( "&_
    " '"&objRs(1)&"', '"&objRs(3)&"', '"&objRs(10)&"',
    '"&objRs(5)&"', '"&objRs(6)&"',"&_
    " '"&objRs(4)&"', N'"&objRs(20)&"', '"&objRs(7)&"',
    '"&objRs(9)&"', '"&objRs(11)&"',"&_
    " '"&objRs(14)&"', N'"&objRs(15)&"', '"&objRs(16)&"',
    '"&objRs(17)&"', '"&objRs(18)&"',"&_
    " '"&objRs(19)&"', getdate(), '"&objRs(13)&"' "&_
    " )"

    ' humm(strSQL)
    objDBStar.execute StrSQL

    objRs.movenext
    Loop

    hum("server Table is done")

    Regards;

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    do you need assistance to understand the "Cannot insert duplicate key" error message?

    it means that only one row per unique value of the PK is allowed
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Oct 2011
    Posts
    9

    primary key violation

    Primary key means that this key must be unique and does not allow null.
    So you must key your input to ensure that the entered value must not violate this constraint.
    I think you must use try catch to catch primary key violation and provide meaningful message to user.


    I hope it helpful.


    Elmozamil Elamir
    Elmozamil
    Last edited by Pat Phelan; 12-27-11 at 19:11.

Posting Permissions

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