If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > How to fix Violation of PRIMARY KEY

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-25-11, 08:15
shafey shafey is offline
Registered User
 
Join Date: Dec 2011
Posts: 1
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

Quote:
'================================================= ========================
' 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;
Reply With Quote
  #2 (permalink)  
Old 12-25-11, 08:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-26-11, 00:08
Elmozamil Elmozamil is offline
Registered User
 
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 18:11.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On