create procedure dbo.%PROC% @sid varchar(30) = null as declare @action_id numeric, @id numeric, @user_id numeric, @state_id numeric, @new_state_id numeric, @type_id numeric DECLARE @deal_in_date DATETIME, @owner_id NUMERIC create table #back_setup_profit_or_loss_temp ( acc_id numeric (18), deb_or_cred int, sum_bal money ) create table #back_setup_sum_bal_acc ( sum_bal money null, qty_sum money null, sum_accur money null, acc_id numeric(18) ) begin tran select @action_id = %ACTION_ID% select @id = convert(numeric, @sid) ---.UPDATE_STATE .ACTION_HISTORY .GET_PROPS .OPERS_ROLL(ALL) declare @pos_id numeric /*-------Проводки ----------*/ -- td_depo_doc2bill - позиции счета -- tb_bill2dealings - сделки, привязанные к позиции declare cur1 cursor for select tb_baysale_docitems.id from tb_baysale_docitems, td_depo_docs, t_types t, t_items i, t_states s, td_depo_doc2bill pos, tb_bill2dealings deals where pos.bill_id = @id and deals.pos2bill_id = pos.id and in_date>=@date_b and in_date<=@date_e and td_depo_docs.id=tb_baysale_docitems.id and i.id=td_depo_docs.id and i.state_id=s.id and s.class_id=0 and t.id=i.type_id and t.type_code in ('DEALINGS_VIA_BROKER','DEALINGS_VIA_BROKER_SALE','DOG_DEALINGS_BAY_VBR','DOG_DEALINGS_SALE_VBR') and deals.item_id = tb_baysale_docitems.id order by in_date, in_no for read only open cur1 fetch cur1 into @pos_id while .CURSOR_STATE=0 begin delete t_items from td_comis2oper where td_comis2oper.id=t_items.id and td_comis2oper.item_id=@pos_id and td_comis2oper.comis_foundation_id=@id and .ITEMS_EXISTS_BY_TYPE(td_comis2oper.id,'TD_COMIS2OPER') -- .ITEMS_EXISTS_BY_TYPE_STATE(td_comis2oper.id,'TD_COMIS2OPER','PODT_LAST') delete td_comis2oper where td_comis2oper.item_id=@pos_id and not exists(select 1 from t_items where t_items.id = td_comis2oper.id) -- .ITEMS_EXISTS_BY_TYPE_STATE(td_comis2oper.id,'TD_COMIS2OPER','PODT_LAST') SELECT @deal_in_date = in_date, @owner_id = owner_id FROM td_depo_docs WHERE id = @pos_id .BACK_SETUP_BAL_SUM(@pos_id, @deal_in_date, @owner_id, 1, 1) fetch cur1 into @pos_id end close cur1 .DEALLOCATE cur1 declare cur2 cursor for select id from td_depo_doc2bill where bill_id=@id for read only open cur2 fetch cur2 into @pos_id while .CURSOR_STATE=0 begin exec ap_can_exec_com_calc null,@pos_id fetch cur2 into @pos_id end close cur2 .DEALLOCATE cur2 commit tran