开发者

INSERT statement conflicted with the FOREIGN KEY constraint after first 1k insert

开发者 https://www.devze.com 2023-02-24 19:33 出处:网络
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 F

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.

0

精彩评论

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

关注公众号