Bonjour,

J'ai voulu contourner mon problème d'index unique sur une vue en modifiant mon modèle des données (ce qui le simplifie donc c'est pas plus mal).

Seulement maintenant j'ai un trigger sur une vue qui n'a plus l'air de vouloir fonctionner...

Voici la table "exploitation.accident" :
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
create table exploitation.accident
(
	id int not null primary key identity,
	num_acc	varchar(12) not null unique,
	dat_acc datetime2 not null,
	lum_id tinyint not null references exploitation.luminosite(id),
	agg_id bit not null references exploitation.agglomeration(id),
	int_id tinyint not null references exploitation.intersection(id),
	atm_id tinyint not null references exploitation.atmosphere(id),
	col_id tinyint not null references exploitation.colision(id),
	com_id varchar(3) not null,
	adr varchar(100) null,
	gps_id char(1) null references exploitation.gps(code),
	position geography null,
	dep_id varchar(3) not null references insee.departement(code),
	foreign key (com_id, dep_id) references insee.commune(code, code_departement)
)

La vue "onisr.caracteristiques_2015" :
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
create view onisr.caracteristiques_2015 (num_acc, an, mois, jour, hrmn, lum, agg, [int], atm, col, com, adr, gps, lat, long, dep)
as
select acc.num_acc, datepart(year, acc.dat_acc), datepart(month, acc.dat_acc), datepart(day, acc.dat_acc), datepart(hour, acc.dat_acc) * 100 + datepart(minute, acc.dat_acc), acc.lum_id, acc.agg_id, acc.int_id, acc.atm_id, acc.col_id, acc.com_id, acc.adr, acc.gps_id, cast(acc.position.Lat * 100000 as int) lat, cast(acc.position.Long * 100000 as int), acc.dep_id
from exploitation.accident acc

Et son trigger :
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
create trigger ins_caracteristiques_2015
on onisr.caracteristiques_2015
instead of insert
as
begin
	insert into exploitation.accident (num_acc, dat_acc, lum_id, agg_id, int_id, atm_id, col_id, com_id, adr, gps_id, position, dep_id)
	select i.num_acc, DATETIME2FROMPARTS(i.an, i.mois, i.jour, cast(i.hrmn / 100 as int), i.hrmn % 100, 0, 0, 0), i.lum, i.agg, case i.[int] when 0 then 1 else i.[int] end, i.atm, i.col, i.com, i.adr, i.gps, case when i.lat is not null and i.long is not null then geography::Point(cast(i.lat as real) / 100000, cast(i.long as real) / 100000, 4326) else null end, case when i.dep = '201' then '2A' when i.dep = '202' then '2B' when i.dep like '0%' then right(i.dep, 2) else i.dep end
	from inserted i;
end

Pourtant, un bulk insert sur la vue me retourne cette erreur :
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
BULK INSERT accidents.onisr.caracteristiques_2015
FROM 'C:\in\Projets\Accidents\AccidentsDatabase\bin\Debug\onisr\caracteristiques_2015.CSV'
WITH (FORMATFILE='C:\in\Projets\Accidents\AccidentsDatabase\bin\Debug\Bulkformats\onisr\caracteristiques_2015.xml', FIRSTROW=2);

Avec le message :
Msg*4406, Niveau*16, État*1, Ligne*1
Impossible de mettre à jour ou d'insérer la vue ou la fonction 'accidents.onisr.caracteristiques_2015' car elle contient un champ dérivé ou constant.
Décidément, quand ça veut pas, ça veut pas...