开发者

'Violation of PRIMARY KEY constraint' SQL Error

This seems to be a pretty common question that is asked but, everything I have tried so far has left with with no solution. I am using an ID field for my PK and auto increment has been turned on. This is occurring in a DEV environment where data has been merged with the current data.

Any help would be greatly appreciated.

I am using a SQL Server. I have also ran DBCC CHECKIDENT([ceschema.ce_attendeeCredit]) and the identity seems to line up properly. I think there may be a deeper issue in play.

INSERT INTO tblpersonCredit
                (
                personID,
                CreditID,
                Amount,
                ReferenceNo,
                CreatedBy
                )
            VALUES
                (
                <cfqueryparam value="#arguments.AttendeeCredit.getAttendeeID()#" CFSQLType="cf_sql_integer" />,
                <cfqueryparam value="#arguments.AttendeeCr开发者_JAVA技巧edit.getCreditID()#" CFSQLType="cf_sql_integer" />,
                <cfqueryparam value="#arguments.AttendeeCredit.getAmount()#" CFSQLType="cf_sql_float" null="#not len(arguments.AttendeeCredit.getAmount())#" />,
                <cfqueryparam value="#arguments.AttendeeCredit.getReferenceNo()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.AttendeeCredit.getReferenceNo())#" />,
                <cfqueryparam value="#arguments.AttendeeCredit.getCreatedBy()#" CFSQLType="cf_sql_integer" />
                )


I noticed this specific line in your question:

This is occurring in a DEV environment where data has been merged with the current data.

After manually inserting data into an autoincrement field you may need to reset the autoincrement counter to something after the maximum id that you inserted.

How you do this (and whether you need to) is database dependent. For example in MySQL you can use an ALTER TABLE statement:

ALTER TABLE tbl AUTO_INCREMENT = 10000

The equivalent command to reset the seed for an SQL Server IDENTITY column is:

DBCC CHECKIDENT(tbl, RESEED, 9999)


Use following logic before Insertion Query.... select MAX(ID) from Table;
string queryInsertStudent = String.Format("insert into tbStudent(studentName,fatherName,programme,session,regNo,enrolNo,emailAddress,contactNo,CNIC,parmanentAddress,Status) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}');", Student_Name, F_Name, Programe, Session, RegNo, EnrollNo, Email, Contact, CNIC, Address, Status); //the above " string queryInsertStudent" is string data type query for data insertion in the data table ClassDatabaseConnection.Excute(queryInsertStudent); //here "Execute is Method under the CLASS "DatabaseConnectionClass"" string student_ID = ClassDatabaseConnection.select("select MAX(studentId) from tbStudent").Rows[0][0].ToString();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜