create procedure dbo.%PROC% @sid varchar(30) = null as .BEGIN('N') begin tran .ID_VAR(@paymt_id) .ID_VAR(@claim_id) declare @check_date datetime select @claim_id=claim_id from tp_extract2claim where id=@id select @check_date=( select ts_reg_report.reg_date from ts_reg_report, tp_extract2claim where tp_extract2claim.id=@id and ts_reg_report.id=tp_extract2claim.extract_id) delete t_items from tp_paymt2pays where t_items.id=tp_paymt2pays.id and tp_paymt2pays.pif_pay_id=@id delete tp_paymt2pays where tp_paymt2pays.pif_pay_id=@id declare paydocs cursor for select p.id from t_paydocs p, t_items i, t_states s, t_types t where i.id = p.id and p.pif_pay_id = @claim_id and i.state_id = s.id and i.type_id = t.id and s.class_id =0 and upper(s.state_code) = 'PERF' and p.complete_date<=@check_date and upper(t.type_code) in ('SD_PAYBANK_IN','SD_PAYDOCS_CASH_IN') and not exists (select 1 from tp_paymt2pays, .ACTUAL_TABLES where tp_paymt2pays.paymt_id=p.id .ACTUAL_WHERE(tp_paymt2pays.id)) open paydocs fetch paydocs into @paymt_id while .CURSOR_STATE = 0 begin .TYPE_INIT('PIF_PAYMT2PAYS') .TYPE_ASSIGN(pif_pay_id,@id) .TYPE_ASSIGN(paymt_id,@paymt_id) .TYPE_INSERT('PIF_PAYMT2PAYS','CRT') fetch paydocs into @paymt_id end close paydocs .DEALLOCATE paydocs commit tran .END