DO $$ DECLARE m_programtype citext; m_ins integer; m_upd integer; m_retval bigint; BEGIN select s.setvalue from core.setting s where s.setname = 'programtype' and nv(s.disableflag) = 0 into m_programtype; --select newid() insert into core.mastertype(category, mastertype, description, inactive, forprefix,guid) select * from ( values ('module','system','Required System Module',0,'','2D4EFDEA-4E7D-4998-8E2A-1F2B67770983') ) r(category, mastertype, description, inactive, forprefix,guid) where not exists ( select 1 from core.mastertype mt where mt.mastertype = r.mastertype and mt.category = r.category ); update core.mastertype u set jsonvalue = _jsonb_object_cat(u.jsonvalue,jsonb_build_object('subtypes',jsonb_build_array('mergetype','mergetargettype'))) where u.category = 'docgentype' and u.mastertype = 'merge' ; ----Merge Target Type with src(category, mastertype, description, inactive, forprefix,guid,rid_parent) as ( select * , ( select mt.rid_mastertype from core.mastertype mt where mt.guid = '5004E9EC-2E4B-4B36-814C-CFD96EFB434B' limit 1 ) --docvault from ( values ('mergetargettype', 'html', 'HTML', 0, '', '479C8772-52C5-418F-B4FD-FA5F511831E8') , ('mergetargettype', 'docx', 'Word Document', 0, '', '4899A6E6-467F-41BE-95EE-C3A3938C57C1') , ('mergetargettype', 'xlsx', 'Excel Document', 0, '', 'AB40EC69-9CEE-4834-A1AC-B317CC47D679') , ('mergetargettype', 'csv', 'Comma Separated Values (CSV)', 0, '', 'D8EE416A-6A40-4F09-80A7-2576278512AA') , ('mergetargettype', 'pdf', 'PDF', 0, '', '3776F161-1BF8-474C-8C1A-097D851D6BF9') , ('mergetargettype', 'same', 'Same as Source', 0, '', 'C7797F0B-5702-4B54-B102-0B610D62730C') ) r(category, mastertype, description, inactive, forprefix, guid) ), ins as ( insert into core.mastertype (category, mastertype, description, inactive, forprefix, guid, rid_parent) select category, mastertype, description, inactive, forprefix, guid, rid_parent from src where not exists ( select 1 from core.mastertype mt2 where mt2.category = src.category and mt2.mastertype = src.mastertype ) returning mastertype.* ), upd as ( update core.mastertype u set description = s.description , inactive = s.inactive , forprefix = s.forprefix , guid = s.guid , rid_parent = s.rid_parent from src s where u.category = s.category and u.mastertype = s.mastertype and ( u.description is distinct from s.description or u.inactive is distinct from s.inactive or u.forprefix is distinct from s.forprefix or u.guid is distinct from s.guid or u.rid_parent is distinct from s.rid_parent ) returning u.* ) select (select count(1) from ins ) ,(select count(1) from upd ) into m_ins, m_upd ; ----Merge Type insert into core.mastertype(category, mastertype, description, inactive, forprefix,guid,rid_parent) select * ,(select mt.rid_mastertype from core.mastertype mt where mt.guid = '5004E9EC-2E4B-4B36-814C-CFD96EFB434B' limit 1) --docvault from ( values ('mergetype','html','HTML Template',0,'','DC3588BC-1865-400D-BE46-8FABE6F9A040') ,('mergetype','docx','Word Document Template',0,'','8535952E-3D9D-4DD4-9BD0-086A8E5A4804') ,('mergetype','xlsx','Excel Document Template',0,'','6678B42F-C06F-4CB6-9003-AF22E80A1295') ,('mergetype','csv','Comma Separated Values (CSV)',0,'','F03F0AC0-D710-4F9D-BB5C-0EAA97245E2A') ,('mergetype','pdf','PDF Fill in Template',1,'','1197CD7A-D2F6-4B01-8B83-E42315E0060C') ,('mergetype','stimulsoft','Stimulsoft Template',0,'','491ECBF2-6AD2-452C-8543-B15C65CB7431') ,('mergetype','sql','SQL Query Template',0,'','74A47C47-2759-4FAB-A2A1-0A42FA87FA46') ,('mergetype','none','None / No Merge',0,'','640B5044-DDCE-4806-A64F-098BE1324693') ) r(category, mastertype, description, inactive, forprefix,guid) where not exists ( select 1 from core.mastertype mt2 where mt2.category = r.category and mt2.mastertype = r.mastertype ); -----eventtype --select newid() with src as ( select r.* , mt.rid_mastertype as rid_parent from ( values ('eventreactioncodetype', 'sql', 'SQL', 0, '', '24AE5A98-024F-4A3D-8BED-9F4A2C410BF9') , ('eventreactioncodetype', 'api', 'API', 1, '', '163A3EC1-6C2A-4462-A15F-D24F953F9CDA') , ('eventreactioncodetype', 'frontend', 'Frontend', 0, '', '50023BB9-A58A-4AE8-AAC9-3126D7122D18') , ('eventreactioncodetype', 'json', 'JSON', 0, '', 'FA331BC2-DCB2-44E0-B835-C62C77A547E3') ) r(category, mastertype, description, inactive, forprefix, guid) cross join ( select mt.rid_mastertype from core.mastertype mt where mt.guid = '2D4EFDEA-4E7D-4998-8E2A-1F2B67770983' limit 1 ) mt ), upd as ( update core.mastertype u set mastertype = src.mastertype ,description = src.description ,inactive = src.inactive ,forprefix = src.forprefix from src where src.guid = u.guid returning * ), ins as ( insert into core.mastertype (category, mastertype, description, inactive, forprefix, guid, rid_parent) select src.category, src.mastertype, src.description, src.inactive, src.forprefix, src.guid, src.rid_parent from src where not exists ( select 1 from core.mastertype mt2 where mt2.category = src.category and mt2.mastertype = src.mastertype ) returning * ) select (select count(1) from ins) + (select count(1) from upd ) into m_retval; END; $$;