我收到以下錯誤:SQL主鍵約束,雖然記錄不存在
Violation of PRIMARY KEY constraint 'PK_ss_student_grade'. Cannot insert duplicate key in object 'dbo.ss_student_grade'. The duplicate key value is (301, 1011, 24801, 33).
如果我檢查插入之前已經有這樣的主鍵沒有記錄表。
插入是通過C#代碼完成的,我確保代碼只運行一次。即使在錯誤後,如果我檢查表,我仍然沒有這樣的主鍵記錄。
注:觸發器在表中插入運行,但它只是寫入日誌文件,並在數據庫
可能混淆什麼SQL以爲已經存在的關鍵不影響任何數據?
編輯:代碼調用執行以下操作
insert into ss_student_grade(sg_school_code,sg_acad_year,sg_serial_no,sg_student_key,sg_original_grade,sg_grade,sg_school_grade,sg_category_type,sg_operator,sg_datetime)
select pg_school_code, pg_acad_year, et_serial_no, mep_student_key, pg_grade,pg_grade,pg_grade,'Original', SYSTEM_USER, @ExamCreationDate
from #StudentGrades
where not exists (select 1 from ss_student_grade where sg_school_code = pg_school_code and sg_acad_year = pg_acad_year and sg_serial_no = et_serial_no and sg_student_key = mep_student_key)
我使用SQL Server 2008 R2和Visual Studio中的程序2010旗艦版
粘貼代碼 – Freelancer 2013-05-13 11:28:15
代碼調用,做如下 \t INSERT INTO ss_student_grade程序(sg_school_code,sg_acad_year,sg_serial_no,sg_student_key, sg_original_grade,sg_grade,sg_school_grade,sg_category_type,sg_operator,sg_datetime) \t選擇pg_school_code,pg_acad_year,et_serial_no,mep_student_key,pg_grade,pg_grade,pg_grade, '原始',SYSTEM_USER,@ExamCreationDate \t從那裏不存在#StudentGrades \t(選擇1來自ss_student_grade,其中sg_school_code = pg_school_code和sg_aca d_year = pg_acad_year和sg_serial_no = et_serial_no和sg_student_key = mep_student_key) – abeyrouthy 2013-05-13 11:31:24
我想'select'返回重複項 – 2013-05-13 11:42:24