select * from dbo.xp_gen_view('dbo.v_lpu',
'
CREATE OR REPLACE VIEW dbo.v_lpu
AS /*WITH reg AS (
         SELECT getregion() AS reg
        )*/
 SELECT lpu.server_id,
    lpu.lpu_id,
    lpu.lpu_sysnick,
    lpu.lpu_isenable,
    org.org_id,
    org.org_code,
    org.org_name AS lpu_name,
    org.org_nick AS lpu_nick,
    org.org_name,
    org.org_nick,
    org.org_email AS lpu_email,
    org.org_www,
    org.org_isemailfixed AS lpu_isemailfixed,
    org.org_phone AS lpu_phone,
    org.uaddress_id,
    au.address_address AS uaddress_address,
    org.paddress_id,
    ap.address_address AS paddress_address,
    lpu.lputype_id,
    lputype.lputype_code,
    lputype.lputype_name,
    lpuperiodoms.lpuperiodoms_regnumc AS lpu_regnomc,
    lpuperiodoms.lpuperiodoms_regnumn AS lpu_regnomn,
    lpu.lpu_regnomc2,
    lpu.lpu_regnomn2,
    lpu.lpu_ouz,
        CASE
            WHEN "substring"(lpu.lpu_ouz::character varying(20)::text, 3, 2) = ''00''::text THEN 2
            ELSE 1
        END AS lpu_isoblast,
        CASE
            WHEN lpuperiodoms.lpuperiodoms_begdate IS NULL THEN 1
            ELSE 2
        END AS lpu_isoms,
    lpuperiodoms.lpuperiodoms_dognum AS lpu_dognum,
    lpu.lpu_dogdate,
    org.org_rukovod AS lpu_glvrach,
    org.org_buhgalt AS lpu_glbuh,
    lpu.lpu_otv,
    lpu.lpu_isindir,
    org.org_ogrn AS lpu_ogrn,
    lpu.lpu_amountpeople,
    lpuperiodoms.lpuperiodoms_begdate AS lpu_omsbegdate,
    lpuperiodoms.lpuperiodoms_enddate AS lpu_omsenddate,
    org.org_okdp AS lpu_okdp,
    lpu.lpu_pensregnum,
    lpu.lpu_regdate,
    lpu.lpu_regnum,
    lpu.lpu_districtrate,
    lpu.lpu_fondequip,
    lpu.lpu_fondosn,
    lpu.lpu_begdate,
    lpu.lpu_enddate,
    lpu.lpulevel_id,
    lpu.lpusubjectionlevel_id,
    lpu.lpu_vizitfact,
    lpu.lpu_koikifact,
    lpu.lpu_farpoint,
    lpu.lpu_naselgor,
    lpu.lpu_naselsel,
    lpu.lpu_naselchild,
    lpu.lpu_naselvzr,
    lpu.lpu_naselrab,
    lpu.lpu_erinfo,
    lpu.lpu_dlobegdate,
    lpu.lpu_dloenddate,
    lpu.lpu_ambulancecount,
    org.org_worktime AS lpu_worktime,
    lpu.pmuser_insid,
    lpu.pmuser_updid,
    lpu.lpu_insdt,
    lpu.lpu_upddt,
    lpu.lpu_ouzold,
    lpu.lpu_f003mcod,
    lpu.lpu_isemergencydir,
        CASE
            WHEN lpuperioddms.lpuperioddms_begdate IS NULL THEN 1
            ELSE 2
        END AS lpu_isdms,
    lpu.mesagelputype_id,
    lpu.lpu_stickaddress,
    lpu.lpu_pid,
    lpu.lpu_nid,
    lpu.org_lid,
    lpu.region_id,
    lpu.lpu_isallowinternetmoderation,
    lpu.lpu_issecret,
    lpu.lpu_docreg,
    lpu.lpu_intercode,
    lpu.lpupmutype_id,
    org.org_okato AS lpu_okato,
    org.org_kbk AS lpu_kbk,
    org.org_sticknick,
    org.org_guid,
    org.oktmo_id,
    lpu.touztype_id,
    lpu.org_tid,
    lpu.lpupmuclass_id,
    lpu.lpulevel_cid,
    lpu.lpu_fssregnum,
    org.org_regname,
    org.org_kpn AS lpu_kpn,
    lpu.lpu_islab,
    lpu.lpu_istest,
    lpu.lpu_hospid,
    lpu.lpu_founder,
    lpu.lpuownership_id,
    lpu.moareafeature_id,
    lpu.lpu_isfrmo,
    lpu.lpubuildingpass_mid,
    lpu.lpu_ismse,
    lpu.lpu_frmosenddt
   FROM lpu
     /*JOIN reg ON COALESCE(lpu.region_id, 0::bigint) = 0 OR COALESCE(lpu.region_id, 0::bigint) = reg.reg*/
     JOIN org ON org.org_id = lpu.org_id
     LEFT JOIN lputype ON lputype.lputype_id = lpu.lputype_id
     LEFT JOIN address au ON org.uaddress_id = au.address_id
     LEFT JOIN address ap ON org.paddress_id = ap.address_id
     LEFT JOIN LATERAL ( SELECT lpuperiodoms_1.lpuperiodoms_begdate,
            lpuperiodoms_1.lpuperiodoms_enddate,
            lpuperiodoms_1.lpuperiodoms_dognum,
            lpuperiodoms_1.lpuperiodoms_regnumc,
            lpuperiodoms_1.lpuperiodoms_regnumn
           FROM lpuperiodoms lpuperiodoms_1
          WHERE lpuperiodoms_1.lpu_id = lpu.lpu_id AND (lpuperiodoms_1.lpuperiodoms_enddate IS NULL OR lpuperiodoms_1.lpuperiodoms_enddate > getdate())
          ORDER BY lpuperiodoms_1.lpuperiodoms_begdate DESC
         LIMIT 1) lpuperiodoms ON true
     LEFT JOIN LATERAL ( SELECT lpuperioddms_1.lpuperioddms_begdate,
            lpuperioddms_1.lpuperioddms_enddate,
            lpuperioddms_1.lpuperioddms_dognum
           FROM lpuperioddms lpuperioddms_1
          WHERE lpuperioddms_1.lpu_id = lpu.lpu_id
          ORDER BY lpuperioddms_1.lpuperioddms_begdate DESC
         LIMIT 1) lpuperioddms ON true
         where lpu.region_id=getregion() or lpu.region_id is null; 
',2)
