-- replace demo data license with the standard license, so all existing standard data
-- will be replaced at the db import
update dt489 set DI041223='011' where DI041223 in ('100', '777');
update dt496 set DI041102='011' where DI041102 in ('100', '777');
update dt497 set DI041116='011' where DI041116 in ('100', '777');
update dt498 set DI041130='011' where DI041130 in ('100', '777');

-- Global settings: default process models
update DT333 set DI051828 = (case when DI051828 is null then null else '011' + REPLICATE('0', 6 - LEN(DI051828)) + DI051828 end)
where DI051831 in ('default_request_model',
                   'default_pm_model_idea',
                   'default_pm_model_intention',
                   'default_pm_model_project')
and DI051828 in (select DI041096 from DT496 where DI041102 in ('011', '100', '777'));

-- Global Variable 157: default process for mod 0099VH
update dt452 set DI003107 = (case when DI003107 is null then null else '011' + REPLICATE('0', 6 - LEN(DI003107)) + DI003107 end)
where DI003109 = 157
and DI003107 in (select DI041123 from DT498 where DI041130 in ('011', '100', '777'));

-- DT499 Objektprozess
alter table DT499 alter column DI041138 varchar (12) not null;
alter table DT499 alter column DI057823 varchar(12);
alter table DT876 alter column DI057863 varchar(12);
alter table DT876 alter column DI057864 varchar(12);

update DT499 set DI057823 = (case when DI057823 is null then null else '011' + DI057823 end),
                 DI041145 = 'MIGRATION', DI041142=GETDATE()
where DI061407 in (select DI041096 from DT496 where DI041102 in ('011', '100', '777'));

update DT876 set DI057863 = (case when DI057863 is null then null else '011' + DI057863 end),
                 DI057871 = 'MIGRATION', DI057868=GETDATE()
where DI057863 in (select DI041138 from DT499 
                    where DI061407 in (select DI041096 from DT496 
                                       where DI041102 in ('011', '100', '777')));

update DT876 set DI057864 = (case when DI057864 is null then null else '011' + DI057864 end),
                 DI057871 = 'MIGRATION', DI057868=GETDATE()
where DI057864 in (select DI041138 from DT499 
                    where DI061407 in (select DI041096 from DT496 
                                       where DI041102 in ('011', '100', '777')));

-- DT877 Objektprozess/Bericht
alter table DT877 alter column DI057896 varchar(12) not null;
alter table DT877 alter column DI057897 varchar(12);

update DT877 set DI057896 = (case when DI057896 is null then null else '011' + DI057896 end),
                 DI057904='MIGRATION', DI057901=GETDATE()
where DI057896 in (select DI041138 from DT499
                   where DI061407 in (select DI041096 from DT496 
                                      where DI041102 in ('011', '100', '777')));

update DT877 set DI057897 = (case when DI057897 is null then null else '011' + DI057897 end),
                 DI057904='MIGRATION', DI057901=GETDATE()
where DI057897 in (select DI041138 from DT499
                   where DI061407 in (select DI041096 from DT496 
                                      where DI041102 in ('011', '100', '777')));

update DT499 set DI041138 = (case when DI041138 is null then null else '011' + DI041138 end),
                 DI041145 = 'MIGRATION', DI041142=GETDATE()
where DI061407 in (select DI041096 from DT496 where DI041102 in ('011', '100', '777'));

-- DT489 Prozessregeln
alter table dt489 alter column DI041217 varchar (12) not null;
alter table DT497 alter column DI059080 varchar(12);
alter table DT498 alter column DI041231 varchar(12);
alter table DT498 alter column DI059088 varchar(12);
alter table DT499 alter column DI057857 varchar(12);
alter table DT499 alter column DI059091 varchar(12);

update DT499 set DI057857 = (case when DI057857 is null then null else '011' + REPLICATE('0', 6 - LEN(DI057857)) + DI057857 end),
                             DI041145='MIGRATION', DI041142=GETDATE()
where DI057857 in (select DI041217 from DT489 where DI041223 in ('011', '100', '777'));

update DT499 set DI059091 = (case when DI059091 is null then null else '011' + REPLICATE('0', 6 - LEN(DI059091)) + DI059091 end),
                             DI041145='MIGRATION', DI041142=GETDATE()
where DI059091 in (select DI041217 from DT489 where DI041223 in ('011', '100', '777'));

-- DT496 Prozessmodell
alter table DT461 alter column DI041200 varchar(12);
alter table DT497 alter column DI041109 varchar(12);
alter table DT496 alter column DI041096 varchar(12) not null;
alter table DT499 alter column DI061407 varchar(12);
alter table DT499 alter column DI057825 varchar(12);

update DT461 set DI041200 = (case when DI041200 is null then null else '011' + REPLICATE('0', 6 - LEN(DI041200)) + DI041200 end)
where DI041200 in (select DI041110 from DT497 
                   where DI041116 in ('011', '100', '777'));

update DT499 set DI061407 = (case when DI061407 is null then null else '011' + REPLICATE('0', 6 - LEN(DI061407)) + DI061407 end),
                 DI041145 = 'MIGRATION', DI041142=GETDATE()
where DI061407 in (select DI041096 from DT496 where DI041102 in ('011', '100', '777'));

update dt499 set DI057825 = (case when DI057825 is null then null else '011' + REPLICATE('0', 6 - LEN(DI057825)) + DI057825 end),
                 DI041145 = 'MIGRATION', DI041142=GETDATE()
where DI061407 in (select DI041096 from DT496 where DI041102 in ('011', '100', '777'));


-- DT497 Phase
alter table DT499 alter column DI041152 varchar(12);
alter table DT877 alter column DI059057 varchar(12);
alter table DT877 alter column DI059057 varchar(12);
alter table DT461 alter column DI059017 varchar(12);
alter table DT498 alter column DI041124 varchar(12);
alter table DT497 alter column DI041110 varchar(12) not null;

update DT461 set DI059017 = (case when DI059017 is null then null else '011' + REPLICATE('0', 6 - LEN(DI059017)) + DI059017 end)
where DI059017 in (select DI041110 from DT497 
                   where DI041116 in ('011', '100', '777'));

update DT499 set DI041152 = (case when DI041152 is null then null else '011' + REPLICATE('0', 6 - LEN(DI041152)) + DI041152 end),
                             DI041145 = 'MIGRATION', DI041142=GETDATE()
where DI041152 in (select DI041110 from DT497 where DI041116 in ('011', '100', '777'));

update DT877 set DI059057 = (case when DI059057 is null then null else '011' + REPLICATE('0', 6 - LEN(DI059057)) + DI059057 end),
                            DI057904='MIGRATION', DI057901=GETDATE()
where DI059057 in (select DI041110 from DT497 where DI041116 in ('011', '100', '777'));

-- DT498 Prozess
alter table DT498 alter column DI041123 varchar(12) not null;
alter table DT498 alter column DI057818 varchar(12);

-- DT876 Historie: Objektprozess
alter table DT876 alter column DI057880 varchar(21) not null;