Declare
  v_dd Date;
  v_ii int;
  v_1570 varchar2(255);
  v_1571 varchar2(255);
  v_1572 varchar2(255);
begin
  select count(*) into v_ii from DEF_VARS_V where VAR_OID in (1001570, 1001571, 1001572);
  if v_ii<>3 then
     raise_application_error(-20001,'#EКонвертация уже проводилась');
  end if;
  select count(*) into v_ii from COM_ANK_AV where ATT in ('XOSEN_RI','XOSEN_RA','XOSEN_RB','XOSEN_RC') and DDB>=:D$DD;
  if v_ii>0 then
     raise_application_error(-20001,'#EВ дате '||to_char(:D$DD,'dd.mm.yyyy')||' или позже есть установленные уровни риска');
  end if;
  select VARVALUE into v_1570 from DEF_VARS_V where VAR_OID=1001570; 
  select VARVALUE into v_1571 from DEF_VARS_V where VAR_OID=1001571; 
  select VARVALUE into v_1572 from DEF_VARS_V where VAR_OID=1001572;
  update DEF_VARS_V set D_END=(:D$DD-1) where VAR_OID in (1001570, 1001571, 1001572); 
  insert into DEF_VARS_V(ID, S_ORG, VAR_OID, VARVALUE, D_BEG, D_END) values (MainID.NextVal, dbo.S_CONTEXT(2), 1001570, 'Низкий', :D$DD, dbo.D4000());
  insert into DEF_VARS_V(ID, S_ORG, VAR_OID, VARVALUE, D_BEG, D_END) values (MainID.NextVal, dbo.S_CONTEXT(2), 1001571, 'Средний', :D$DD, dbo.D4000());
  insert into DEF_VARS_V(ID, S_ORG, VAR_OID, VARVALUE, D_BEG, D_END) values (MainID.NextVal, dbo.S_CONTEXT(2), 1001572, 'Высокий', :D$DD, dbo.D4000());
  for q in (select A.ID, 
                   GS_COM_ANK_AV('XOSEN_RI', A.ID, :D$DD) as XOSEN_RI,
                   GS_COM_ANK_AV('XOSEN_RA', A.ID, :D$DD) as XOSEN_RA,
                   GS_COM_ANK_AV('XOSEN_RB', A.ID, :D$DD) as XOSEN_RB,
                   GS_COM_ANK_AV('XOSEN_RC', A.ID, :D$DD) as XOSEN_RC
              from COM_ANKETA A where A.S_S=1 and A.ID in (select VNESHID from COM_ANK_AV where ATT='XOSEN_RI' and trim(VAL_STR) is not NULL)) loop
      if q.XOSEN_RI in (v_1570, v_1571, v_1572) then
        insert into COM_ANK_AV(ID, S_ORG, VNESHID, DDB, ATT, VAL_STR) 
             values (MainID.NextVal, dbo.S_CONTEXT(2), q.ID, :D$DD, 'XOSEN_RI',
                     case when q.XOSEN_RI = v_1570 then 'Низкий' 
                          when q.XOSEN_RI = v_1571 then 'Средний' 
                          when q.XOSEN_RI = v_1571 then 'Высокий'
                     end);
      end if;               
      if q.XOSEN_RA in (v_1570, v_1571, v_1572) then
        insert into COM_ANK_AV(ID, S_ORG, VNESHID, DDB, ATT, VAL_STR) 
             values (MainID.NextVal, dbo.S_CONTEXT(2), q.ID, :D$DD, 'XOSEN_RA',
                     case when q.XOSEN_RA = v_1570 then 'Низкий' 
                          when q.XOSEN_RA = v_1571 then 'Средний' 
                          when q.XOSEN_RA = v_1571 then 'Высокий'
                     end);
      end if;               
      if q.XOSEN_RB in (v_1570, v_1571, v_1572) then
        insert into COM_ANK_AV(ID, S_ORG, VNESHID, DDB, ATT, VAL_STR) 
             values (MainID.NextVal, dbo.S_CONTEXT(2), q.ID, :D$DD, 'XOSEN_RB',
                     case when q.XOSEN_RB = v_1570 then 'Низкий' 
                          when q.XOSEN_RB = v_1571 then 'Средний' 
                          when q.XOSEN_RB = v_1571 then 'Высокий'
                     end);
      end if;               
      if q.XOSEN_RC in (v_1570, v_1571, v_1572) then
        insert into COM_ANK_AV(ID, S_ORG, VNESHID, DDB, ATT, VAL_STR) 
             values (MainID.NextVal, dbo.S_CONTEXT(2), q.ID, :D$DD, 'XOSEN_RC',
                     case when q.XOSEN_RC = v_1570 then 'Низкий' 
                          when q.XOSEN_RC = v_1571 then 'Средний' 
                          when q.XOSEN_RC = v_1571 then 'Высокий'
                     end);
      end if;               
  end loop;
end;  
