create view personnes
 (
  nom, sans_logement, isolement_social, sans_emploi, mucovicidose, pieds_plats
 )
 as
 select pe.nom, a1.affection, a2.affection, a3.affection, a4.affection, a5.affection
 from personnes_new pe   left outer join affections a1 on a1.id_personne = pe.id and a1.id_probleme = 1
 left outer join affections a2 on a2.id_personne = pe.id and a2.id_probleme = 2
 left outer join affections a3 on a3.id_personne = pe.id and a3.id_probleme = 3
 left outer join affections a4 on a4.id_personne = pe.id and a4.id_probleme = 4
 left outer join affections a5 on a5.id_personne = pe.id and a5.id_probleme = 5;
   go
   create trigger personnes_ins
 on dbo.personnes
 instead of insert
 as
  -- Inserted values
  declare @nom    as varchar(50);
  declare @sans_logement  as bit;
  declare @isolement_social	as bit;
  declare @sans_emploi  as bit;
  declare @mucovicidose  as bit;
  declare @pieds_plats  as bit;
    -- Internal values
  declare @pers_id  	as numeric;
 begin
  declare cur_ins cursor
  for
  select nom, sans_logement, isolement_social, sans_emploi, mucovicidose, pieds_plats
  from inserted;
    open cur_ins;
    fetch next from cur_ins
  into @nom, @sans_logement, @isolement_social, @sans_emploi, @mucovicidose, @pieds_plats;
    begin transaction;
    while @@FETCH_STATUS = 0
  begin
  	if exists (select null from personnes_new where nom = @nom)
  	begin
    rollback;
    raiserror('Can''t duplicate name "%s". This is a limitation from old model.', 16, 1, @nom);
    return;
  	end;
    	insert into personnes_new (nom) values (@nom);
  	set @pers_id = scope_identity();
    	if @sans_logement is not null
  	begin
    insert into affections (id_personne, id_probleme, affection) values (@pers_id, 1, @sans_logement);
  	end;
    	if @isolement_social is not null
  	begin
    insert into affections (id_personne, id_probleme, affection) values (@pers_id, 2, @isolement_social);
  	end;
    	if @sans_emploi is not null
  	begin
    insert into affections (id_personne, id_probleme, affection) values (@pers_id, 3, @sans_emploi);
  	end;
    	if @mucovicidose is not null
  	begin
    insert into affections (id_personne, id_probleme, affection) values (@pers_id, 4, @mucovicidose);
  	end;
    	if @pieds_plats is not null
  	begin
    insert into affections (id_personne, id_probleme, affection) values (@pers_id, 5, @pieds_plats);
  	end;
    	fetch next from cur_ins
  	into @nom, @sans_logement, @isolement_social, @sans_emploi, @mucovicidose, @pieds_plats;
  end;
    commit;
    close cur_ins;
  deallocate cur_ins;
 end;
   go
   create trigger personnes_upd
 on dbo.personnes
 instead of update
 as
  -- Inserted values
  declare @i_nom    as varchar(50);
  declare @i_sans_logement  as bit;
  declare @i_isolement_social	as bit;
  declare @i_sans_emploi  as bit;
  declare @i_mucovicidose  as bit;
  declare @i_pieds_plats  as bit;
    -- Deleted values
  declare @d_nom    as varchar(50);
  declare @d_sans_logement  as bit;
  declare @d_isolement_social	as bit;
  declare @d_sans_emploi  as bit;
  declare @d_mucovicidose  as bit;
  declare @d_pieds_plats  as bit;
    -- Internal values
  declare @pers_id  	as numeric;
 begin
  declare cur_ins cursor
  for
  select nom, sans_logement, isolement_social, sans_emploi, mucovicidose, pieds_plats
  from inserted;
    declare cur_del cursor
  for
  select nom, sans_logement, isolement_social, sans_emploi, mucovicidose, pieds_plats
  from deleted;
    open cur_ins;
  open cur_del;
    fetch next from cur_ins
  into @i_nom, @i_sans_logement, @i_isolement_social, @i_sans_emploi, @i_mucovicidose, @i_pieds_plats;
    fetch next from cur_del
  into @d_nom, @d_sans_logement, @d_isolement_social, @d_sans_emploi, @d_mucovicidose, @d_pieds_plats;
    begin transaction;
    while @@FETCH_STATUS = 0
  begin
  	select @pers_id = id
  	from personnes_new
  	where nom = @d_nom;
    	if (@i_nom != @d_nom)
  	begin
    if exists (select null from personnes_new where nom = @i_nom)
    begin
    	rollback;
    	raiserror('Can''t duplicate name "%s". This is a limitation from old model.', 16, 1, @i_nom);
    	return;
    end;
      update personnes_new set nom = @i_nom
    where nom = @d_nom;
  	end;
    	if @i_sans_logement is not null
  	begin
    if @d_sans_logement is not null
    begin
    	update affections
    	set affection = @i_sans_logement
    	where id_personne = @pers_id
    	and id_probleme = 1;
    end;
    else
    begin
    	insert into affections (id_personne, id_probleme, affection)      	values (@pers_id, 1, @i_sans_logement);
    end;
  	end;
  	else
  	begin
    delete affections      where id_personne = @pers_id      and id_probleme = 1;
  	end;
    	if @i_isolement_social is not null
  	begin
    if @d_isolement_social is not null
    begin
    	update affections
    	set affection = @i_isolement_social
    	where id_personne = @pers_id
    	and id_probleme = 2;
    end;
    else
    begin
    	insert into affections (id_personne, id_probleme, affection)      	values (@pers_id, 2, @i_isolement_social);
    end;
  	end;
  	else
  	begin
    delete affections      where id_personne = @pers_id      and id_probleme = 2;
  	end;
    	if @i_sans_emploi is not null
  	begin
    if @d_sans_emploi is not null
    begin
    	update affections
    	set affection = @i_sans_emploi
    	where id_personne = @pers_id
    	and id_probleme = 3;
    end;
    else
    begin
    	insert into affections (id_personne, id_probleme, affection)      	values (@pers_id, 3, @i_sans_emploi);
    end;
  	end;
  	else
  	begin
    delete affections      where id_personne = @pers_id      and id_probleme = 3;
  	end;
    	if @i_mucovicidose is not null
  	begin
    if @d_mucovicidose is not null
    begin
    	update affections
    	set affection = @i_mucovicidose
    	where id_personne = @pers_id
    	and id_probleme = 4;
    end;
    else
    begin
    	insert into affections (id_personne, id_probleme, affection)      	values (@pers_id, 4, @i_mucovicidose);
    end;
  	end;
  	else
  	begin
    delete affections      where id_personne = @pers_id      and id_probleme = 4;
  	end;
    	if @i_pieds_plats is not null
  	begin
    if @d_pieds_plats is not null
    begin
    	update affections
    	set affection = @i_pieds_plats
    	where id_personne = @pers_id
    	and id_probleme = 5;
    end;
    else
    begin
    	insert into affections (id_personne, id_probleme, affection)      	values (@pers_id, 5, @i_pieds_plats);
    end;
  	end;
  	else
  	begin
    delete affections      where id_personne = @pers_id      and id_probleme = 5;
  	end;
    	fetch next from cur_ins
  	into @i_nom, @i_sans_logement, @i_isolement_social, @i_sans_emploi, @i_mucovicidose, @i_pieds_plats;
    	fetch next from cur_del
  	into @d_nom, @d_sans_logement, @d_isolement_social, @d_sans_emploi, @d_mucovicidose, @d_pieds_plats;
  end;
    commit;
    close cur_ins;
  deallocate cur_ins;
    close cur_del;
  deallocate cur_del;
 end;
   go
   create trigger personnes_del
 on dbo.personnes
 instead of delete
 as
  -- Deleted values
  declare @nom    as varchar(50);
    -- Internal values
  declare @pers_id  	as numeric;
 begin
  declare cur_del cursor
  for
  select nom
  from deleted;
    open cur_del;
    fetch next from cur_del
  into @nom;
    begin transaction;
    while @@FETCH_STATUS = 0
  begin
  	select @pers_id = id
  	from personnes_new
  	where nom = @nom;
    	delete affections
  	where id_personne = @pers_id;
    	delete personnes_new
  	where id = @pers_id;
    	fetch next from cur_del
  	into @nom;
  end;
    commit;
    close cur_del;
  deallocate cur_del;
 end;
   go
   |