IF exists(select link from rem1 where object= dbo.obj_id('documents_charge'))
BEGIN
INSERT INTO	EXTREM(rem1,number_el,number,date,date_exec,pay_date,date_acpt,summa,ccorr1,cppu1,corr_n1,note1,corr_n2,row_id,paystatus)
select
	[rem1]   = e.rem1,
	[number_el] = left(e.[el_num], 16),
    [number]       = e.number,
    [rdate]        = e.[rdate],
    [date_exec]    = e.date_exec,
    [pay_date]       = e.date_b,
    [date_acpt]       = e.date_e,
    [summa]        = e.summa,
    [ccorr1]       = e.ccorr1,
    [cppu1]        = e.cppu1,
    [corr_n1]      = e.corr_n1,
	[note1]        = e.note1,
	[corr_n2]          = left(e.isp, 100),
	[row_id]	= e.row_id,
	'1'
from Charge e  

update rem1 set
  object = dbo.obj_id('documents_adm_nach')
where object= dbo.obj_id('documents_charge') 

update e set
  e.rem3 = r3.link
from extrem e
  inner join rem1 r1 on r1.link=e.rem1 and r1.object=dbo.obj_id('DOCUMENTS_ADM_NACH')
  inner join rem2 r2 on r2.link_up=r1.link
  inner join rem3 r3 on r3.link_up=r2.link  

update te set
  te.extrem_in = (select e.link from extrem e where e.rem1=te.rem1)
from t_extrem te
where te.object=dbo.obj_id('CHAIN_ADMNACH_PC') 
END

GO





