I am attempting to insert about 30k rows of data into a database. The first thousand rows go through without any problem but after that I keep getting the error "INSERT statement conflicted with the FOREIGN KEY constraint"
I am inserting into one table first getting the identity and then using that as a fk for the other table. Code looks like
insert in开发者_如何学运维to PYMT_HIST(PAR_NUM,BILL_NUM,RCPT_NUM,TAX_YR,TOT_AMT_DUE,TOT_PD,PD_DATE,PD_BY,MUNI_DIST_ID)
values (@par_num,@BILL_NUM,@RCTP_NUM,@TAX_YR,@TOT_AMT_DUE,@TOT_PD,(Convert(datetime,@PD_DATE)),@PD_BY,@MUNI_DIST_ID);
set @pymt_hist_id = @@identity
IF @REAL_ESTATE > 0
insert into PYMT_HIST_DETL(PYMT_HIST_ID,AMT_PD,FIN_TXN_LN_TY_ID)
values(@pymt_hist_id,@REAL_ESTATE,1);
Try using Scope_Identity() function instead of @@Identity. @@Identity has global scope and could be getting changed outside the scope of the work you are doing.
精彩评论