Re: Primary key error
- From: "Russell Fields" <russellfields@xxxxxxxxxx>
- Date: Tue, 26 May 2009 13:20:38 -0400
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
.
- Follow-Ups:
- Re: Primary key error
- From: Eric
- Re: Primary key error
- References:
- Primary key error
- From: Eric
- Primary key error
- Prev by Date: Re: SQL Server Assertion error 17066
- Next by Date: cannot add contents of one table to another
- Previous by thread: Primary key error
- Next by thread: Re: Primary key error
- Index(es):
Relevant Pages
|