HKFuey
asked on
ASP SQL Syntax Duplicate Key
I have a table tbl_SerialNumbers the Primary key is DispatchNote and Serial. I am using the following to update but get a primary key error "[Microsoft][SQL Server Native Client 10.0][SQL Server]Violation of PRIMARY KEY constraint 'PK_tbl_SerialNumbers_1'. Cannot insert duplicate key in object 'dbo.tbl_SerialNumbers'. The duplicate key value is (000000000601437, 33132132132 )."
This happens even though the values are not already in the table! I have tested typing directly into the table and that works fine.
Can anyone help?
This happens even though the values are not already in the table! I have tested typing directly into the table and that works fine.
SqlStr ="SELECT DispatchNote, Serial, UsrUpdated, StockCode, SalesOrder, Updated FROM dbo.tbl_SerialNumbers "
SqlStr = SqlStr & "Where DispatchNote = '" & Dnote & "' And Serial = '" & Seri & "'"
Set Rst = dbConnect.Execute(SQLstr)
If Rst.EOF = true then
SqlStr = "Update tbl_SerialNumbers "
SqlStr = SqlStr & "SET DispatchNote ='" & Dnote & "', Serial = '" & Seri & "', UsrUpdated = '" & Usr & "', SalesOrder = '" & Sord & "', Updated = getdate()"
Set Rst = dbConnect.Execute(SQLstr)
end if
Can anyone help?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ok, let me walk though the code:
1. The code first executes the SELECT using the DispatchNote and Serial value
2. If you don't get a resultset (Rst.EOF = true), then
3. The code is executing an - "Update tbl_SerialNumbers...."
So, first thing would be to change the UPDATE to an INSERT. You need a query like:
Also, can you confirm the datatypes for Rst, dbConnect and SqlStr?
1. The code first executes the SELECT using the DispatchNote and Serial value
2. If you don't get a resultset (Rst.EOF = true), then
3. The code is executing an - "Update tbl_SerialNumbers...."
So, first thing would be to change the UPDATE to an INSERT. You need a query like:
If Rst.EOF = true then
SqlStr = "INSERT INTO tbl_SerialNumbers (DispatchNote, Serial, UsrUpdated, SalesOrder, Updated) "
SqlStr = SqlStr & "VALUES ('" & Dnote & ", " & Seri & ", " & Usr & ", " & Sord & ", getdate())"
Set Rst = dbConnect.Execute(SQLstr)
end if
Also, can you confirm the datatypes for Rst, dbConnect and SqlStr?
ASKER
Thanks guys, Gave Nakul point as he made me realize where I was going wrong!
Also, the Primary Key violation tells us that the record already exists. So, do check if you have any duplicate code that is also being executed at the same time that goes ahead and inserts the row.
Essentially, you only need to supply the values for the primary key during an INSERT - in all other cases (SELECT/UPDATE/DELETE), these values will be in the WHERE clause.
Essentially, you only need to supply the values for the primary key during an INSERT - in all other cases (SELECT/UPDATE/DELETE), these values will be in the WHERE clause.
ASKER
Thanks Nakul, I would give you more points but I already closed the questiomnj ;)
ASKER
This is not an 'update' just adding an entirely new row.