
                    DO $$
                    BEGIN
                        IF NOT EXISTS(select * from pg_prepared_statements where name = 'prmd331d03d11522492bfc6d120b18febee1') THEN
                        PREPARE prmd331d03d11522492bfc6d120b18febee1 as
                        
            -- TOP SL, FIXDATE 09.02.2023, JIRA PROMEDWEB-141522
            select
                Evn.Evn_id as "Evn_id",
                Evn.Evn_pid as "Evn_pid",
                Evn.Evn_rid as "Evn_rid",
                Evn.EvnClass_id as "EvnClass_id",
                Evn.EvnClass_SysNick as "EvnClass_SysNick",
                EvnXml.EvnXml_id as "EvnXml_id",
                EvnXml.Evn_id as "Evn_id_xml",
                EvnXml.XmlType_id as "XmlType_id",
                EvnXml.EvnXml_Name as "EvnXml_Name",
                EvnXml.EvnXml_Data as "EvnXml_Data",
                xts.XmlTemplateSettings_Settings as "XmlTemplate_Settings",
                xth.XmlTemplateHtml_HtmlTemplate as "XmlTemplate_HtmlTemplate",
                xtd.XmlTemplateData_Data as "XmlTemplate_Data",
                EvnXml.XmlTemplate_id as "XmlTemplate_id",
                EvnXml.XmlTemplateType_id as "XmlTemplateType_id",
                EvnXml.EvnXml_IsSigned as "EvnXml_IsSigned",
                $1 as "instance_id"
            from v_EvnXml EvnXml
                inner join lateral (select * from v_Evn where Evn_id = EvnXml.Evn_id limit 1) Evn on true
                left join XmlTemplateData xtd on xtd.XmlTemplateData_id = EvnXml.XmlTemplateData_id
                left join XmlTemplateHtml xth on xth.XmlTemplateHtml_id = EvnXml.XmlTemplateHtml_id
                left join XmlTemplateSettings xts on xts.XmlTemplateSettings_id = EvnXml.XmlTemplateSettings_id
            where EvnXml.Evn_id = $2 and EvnXml.XmlType_id in (4,7) and EvnXml.XmlTemplate_id is null
            order by EvnXml.EvnXml_updDT desc
            limit 1
        ;
                        END IF;
                    END $$;
                    EXECUTE prmd331d03d11522492bfc6d120b18febee1 ('192785382364ba4f962ddb3','560101106008454');
                
