Re: Primary key error

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

Each column of data is just a VARCHAR store, except the one for ID, that is
a BIGINT with auto numbering.
The column names as used in the code below, are put in as strings.
So: GeneralLog.YPcode is a string for "yp_code". I use this to be
constistent with column names thoughout my code.

So to explain again:

Step 1: I read the data from the sql server with a basic SELECT command.
It's put in a dataset and then linked to textboxes and checkboxes.
step 2: the user changes the data
step 3: When the user clicks on the finish button, the program first makes a
backup for each row where the column YPCODE has the value of the read data
from step 1.
step 4: the changed data is written to the sql server as a new record.

So this means after these steps I would have two records in the tables, ie:
columns: YPcode, data
record 1: test_BAK, testdata
record 2: test, changedtestdata

For the tables where YPcode is the primary key, this is not a problem, but I
have some tables which are allowed to have more then one line for the same
YPcode, so they have an extra column ID, which is an Identy column with
autoincrement of 1.
When inserting the new record with the changed data into these tables, gives
the exception error:

{"Violation of PRIMARY KEY constraint 'PK__OverallS__3214EC2775435199'.
Cannot insert duplicate key in object 'dbo.OverallStatus'. The statement has
been terminated."}

My program stops then in de debug mode, if I then step it through, the
record is added and it continues fine.

It seems like I need to wait (?) or do some other command to have the server
deal with the record?
Do I have to do something like for a dataset: acceptchanges?

how do I solve this?

rg,
Eric




"Russell Fields" wrote:

Eric,

Your notes below do not make it clear to me what you are doing with each
column of data.

I would suggest that you use SQL Server Profiler to trace a problem case and
capture the SQL statements that are issued. I see that you are building a
string of SQL code for the update, so if you create the INSERT the same way
the profiler trace should show all the parameters that you are inserting.

Looking at that might help you.

To help somebody else duplicate your problem, follow the guidelines at:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

RLF

"Eric" <Eric@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:D1637D50-833A-4E34-A917-38C2B48010C4@xxxxxxxxxxxxxxxx
Hi,

In my VB.NET program the user can update a bunch of information.
Just before the update is put in the database, I first change one field by
putting _bak behind the text with:

mySQL = "UPDATE " + Project.GeneralLog + " SET " + GeneralLog.YPcode + " =
" + GeneralLog.YPcode + "+ '_BAK' "
mySQL += "WHERE " + GeneralLog.YPcode + " = '" + ypcode + "'"

myCMD = New SqlCommand(mySQL, conn)
Try
myCMD.ExecuteNonQuery()
Catch ae As SqlException
conn.Close()
Return False
End Try

After that I use INSERT TO to put the the changed data in.
In most tables it goes ok because the primary key is the field that gets
changed, but in the tables that have a seperate ID field to be the primary
key (because the field that is primary in the other tables can have double
entries in this table).

For these tables I get an error:

{"Violation of PRIMARY KEY constraint 'PK__OverallS__3214EC2775435199'.
Cannot insert duplicate key in object 'dbo.OverallStatus'. The statement
has
been terminated."}

The table is created with:

mySQL = "CREATE TABLE " + Project.OverallStatus + " (ID BIGINT
IDENTITY(1,1) PRIMARY KEY, " + OverallStatus.YPcode + " VARCHAR(15), "
mySQL += OverallStatus.Datum + " DATETIME, "
mySQL += OverallStatus.Description + " VARCHAR(5000))"
myCMD = New SqlCommand(mySQL, conn)
Try
myCMD.ExecuteNonQuery()
Catch ae As SqlException
nOK = True
End Try

The INSTERT INTO command I use, is the same one as for putting the data in
the table in the first time, so that is working properly.

What am I doing wrong?

rg,
Eric



.



Relevant Pages

  • Re: Primary key error
    ... If you are updating the table with the composite primary key then both columns in the PK need to be referenced in the UPDATE statement. ... I read the data from the sql server with a basic SELECT command. ... backup for each row where the column YPCODE has the value of the read data ...
    (microsoft.public.sqlserver.server)
  • Re: PK violation error during replication
    ... Wayne Snyder, MCDBA, SQL Server MVP ... > Violation of PRIMARY KEY ... > duplicate key in object '#65EA00FF'. ... > me the hot fix, ...
    (microsoft.public.sqlserver.server)
  • Re: Clustered Index question
    ... Columnist, SQL Server Professional ... If I keep the primary key, ... >select packet_time,packet_data from packet where ... >have the clustered index (either on PACKET_TIME or on ...
    (microsoft.public.sqlserver.programming)
  • A pk is *both* a physical and a logical object.
    ... Primary key is a logical constraint. ... but SQL Server does not allow you to create a unique or primary ... constraints) by indexing is purely *platform dependent*. ... PKs are part of the ANSI SQL standard *for db implementations*. ...
    (comp.databases.theory)
  • Re: Re-Seed in SQL Server
    ... Add a new field to the table that holds the Primary Key. ... or something like that and make it an Autonumber field and a Primary Key. ... If the table is already on the SQL Server you could try the below. ... that your Autonumber field in the Access Database has gotten corrupted. ...
    (microsoft.public.access.tablesdbdesign)