INSERT INTO  [IES].[T_F002_SMO]
           (
       [TF_OKATO]
      ,[SMOCOD]
      ,[NAM_SMOP]
      ,[NAM_SMOK]
      ,[INN]
      ,[OGRN]
      ,[KPP]
      ,[ADDR_J]
      ,[ORG]
      ,[FAM_RUK]
      ,[IM_RUK]
      ,[OT_RUK]
      ,[PHONE]
      ,[FAX]
      ,[E_MAIL]
      ,[N_DOC]
      ,[D_START]
	  ,[DATA_E]
	  ,IsActive
      ,[ADDR_F]
      ,[KOL_ZL])

select  
 f.[TF_OKATO]
,f.TF_COD + '000'
,''
,''
,''
,''
,''
,''
,0
,''
,''
,''
,''
,''
,''
,''
,'20260101'
,'20991231'
,1
,''
,0
from ies.T_F019_PERS_ACC_ORG f
where dbo.RegExMatch(upper(f.TF_OKATO),'[0-9][0-9]000') = 1
and len(f.TF_COD)>1
and not exists(select * from ies.T_F002_SMO f002 where f002.SMOCOD = f.TF_COD + '000')
group by f.[TF_OKATO],f.TF_COD
order by f.TF_COD

IF (not exists(select * from ies.T_F002_SMO f002 where f002.SMOCOD = '99000'))
BEGIN

INSERT INTO  [IES].[T_F002_SMO]
           (
       [TF_OKATO]
      ,[SMOCOD]
      ,[NAM_SMOP]
      ,[NAM_SMOK]
      ,[INN]
      ,[OGRN]
      ,[KPP]
      ,[ADDR_J]
      ,[ORG]
      ,[FAM_RUK]
      ,[IM_RUK]
      ,[OT_RUK]
      ,[PHONE]
      ,[FAX]
      ,[E_MAIL]
      ,[N_DOC]
      ,[D_START]
	  ,[DATA_E]
	  ,IsActive
      ,[ADDR_F]
      ,[KOL_ZL])

select  
 '90000'
,'99' + '000'
,''
,''
,''
,''
,''
,''
,0
,''
,''
,''
,''
,''
,''
,''
,'20260101'
,'20991231'
,1
,''
,0
end