--
-- ### delete with nested subselect
-- 
-- this is just an extract of the problem code and in this way really makes no sense 
--

drop if exists table ma_sysworkflow;
create table ma_sysworkflow(sysid int not null, bgid int not null, seqnr int not null, wfaction string(100), assigned datetime, released datetime, logname string(100), wfcomment string(500), status string(10) not null);
-- drop if exists btree ma_sysworkflow_pbtree;
-- create primary btree  on ma_sysworkflow(sysid,bgid,seqnr);

insert into ma_sysworkflow values (1749, 1, 5, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1749, 5, 4, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1749, 1, 2, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1756, 1, 5, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1749, 1, 3, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1488, 1, 2, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1488, 1, 3, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1488, 1, 5, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1488, 5, 4, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1489, 1, 2, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1489, 1, 3, 'XXXXXXXX', null, null, null, null, 'Wait');
insert into ma_sysworkflow values (1489, 1, 5, 'XXXXXXXX', null, null, null, null, 'Wait');

drop if exists table ma_host;
create table ma_host(hostid int not null, hostname string(50) not null, hostip string(50), hostdesc string(500), pfid int, apacheversion string(100), osversion string(100));
drop if exists btree ma_host_b1;
create btree ma_host_b1 on ma_host(pfid);
drop if exists btree ma_host_pbtree;
create primary btree  on ma_host(hostid);

insert into ma_host values (1866, 'XXXXXXXX', null, null, 131, 'NA', 'NA');
insert into ma_host values (1867, 'XXXXXXXX', null, null, 131, 'NA', 'NA');
insert into ma_host values (1868, 'XXXXXXXX', null, null, 127, 'NA', 'NA');
insert into ma_host values (1869, 'XXXXXXXX', null, null, 128, 'NA', 'NA');
insert into ma_host values (1870, 'XXXXXXXX', null, null, 128, 'NA', 'NA');
insert into ma_host values (1915, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1960, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1957, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1958, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1959, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1961, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1962, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1963, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1965, 'XXXXXXXX', null, null, 194, 'GENERIC', 'NA');
insert into ma_host values (1964, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1966, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1967, 'XXXXXXXX', null, null, 194, null, 'NA');
insert into ma_host values (1783, 'XXXXXXXX', 'NA', null, 57, 'NA', 'NA');
insert into ma_host values (1771, 'XXXXXXXX', null, null, 8, null, 'Test');
insert into ma_host values (1769, 'XXXXXXXX', null, null, 8, null, 'Production');

drop if exists table ma_system;
create table ma_system(sysid int not null, sysname string(100) not null, title string(100), ordernumber string(100), customerinfo string(100), usageinfo string(100), netzone string(100), sysstat string(50) not null, sysdelivery string(50), sysexpire string(50), sysdesc string(1000), createdat datetime, orderedby string(100), tpid int not null, sysrole string(50), hostname string(100), hostid int not null, javaversion string(100), tomcatversion string(100), sysupdate datetime);

drop if exists btree ma_system_pbtree;
create primary btree  on ma_system(sysid);
drop if exists btree ma_system_b1;
create btree ma_system_b1 on ma_system(hostid);

insert into ma_system values (1974, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2122, null, null, null);
insert into ma_system values (1975, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2125, null, null, null);
insert into ma_system values (1976, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2231, null, null, null);
insert into ma_system values (1977, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2093, null, null, null);
insert into ma_system values (1978, 'apache-2', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2093, null, null, null);
insert into ma_system values (1979, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2097, null, null, null);
insert into ma_system values (1980, 'apache-2', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2097, null, null, null);
insert into ma_system values (1981, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2094, null, null, null);
insert into ma_system values (1762, 'MA-Test32', 'NA', 'NA', null, 'XXXX', 'Intranet', 'Created', null, null, 'xxx', scandate('%d.%m.%Y %H:%M:%S','30.05.2017 16:36:40'), 'lemke', 3, 'development', null, 2059, 'GENERIC', null, null);
insert into ma_system values (1982, 'apache-2', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2094, null, null, null);
insert into ma_system values (1983, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2095, null, null, null);
insert into ma_system values (1984, 'apache-2', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2095, null, null, null);
insert into ma_system values (1985, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2098, null, null, null);
insert into ma_system values (1986, 'apache-2', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2098, null, null, null);
insert into ma_system values (1987, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 1972, null, null, null);
insert into ma_system values (1988, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2062, null, null, null);
insert into ma_system values (1989, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2063, null, null, null);
insert into ma_system values (1926, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2228, null, null, null);
insert into ma_system values (1927, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2208, null, null, null);
insert into ma_system values (1928, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2209, null, null, null);
insert into ma_system values (1929, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2138, null, null, null);
insert into ma_system values (1930, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 8, null, null, 2226, null, null, null);
insert into ma_system values (1931, 'apache-1', null, null, null, null, null, 'Ordered', null, null, null, null, null, 9, null, null, 2083, null, null, null);
insert into ma_system values (1932, 'apache-2', null, null, null, null, null, 'Ordered', null, null, null, null, null, 9, null, null, 2083, null, null, null);

drop if exists procedure deletePlatform;
@
create procedure deletePlatform(pfid in int)
begin

  delete from ma_sysworkflow where sysid in ( select sysid as sysid
              from
              ma_system ma_system
              where hostid in ( select hostid as hostid
                                from
                                ma_host ma_host
                                where pfid = :pfid ) );
end;
@

call deletePlatform(199);
call deletePlatform(223);
call deletePlatform(222);
