Bonsoir,

J'ai 2 tables : FACTURE (Tempo_1 dans le script) et ENCHERES
Un trigger sur INSERT positionné sur la table FACTURE qui doit mettre à jour un champ MONTANT MAJORE dans certains enregistrements de la table ENCHERE en fonction de jointures et variables définis dans mon trigger.

Si j'insère un seul enregistrement dans la table FACTURE tout se passe bien.
Par contre si j'insère un lot d'enregistrements, un seul enregistrement est correctement mis à jour dans ma table ENCHERES.

Dois-je utiliser la table INSERTED, vu que les curseurs sont impossible dans un trigger, pour faire en sorte que tous mes enregistrements insérés dans FACTURE déclenchent la mise à jour correspondante dans ENCHERES ?

FOR EACH ROW ?

Merci d'avance pour vos lumières ....

Voici le script :

Code : 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
 
 
ALTER Trigger [dbo].[Calcul_montants_majores] 
On [dbo].[Tempo_1]
FOR INSERT
As
/* Date dernière Révision: 14/10/2007*/
 
--Déclaration des variables
DECLARE
@refpiece nvarchar (50),
@numb int,
@prixdepart money,
@montant_avcom money,
@reflot nvarchar(15),
@idoffre int
--Déclaration de la Transaction
DECLARE @TranName VARCHAR(20)
SELECT @TranName ='Test'
 
--Début de la Transaction
BEGIN TRANSACTION  @TranName
 
--Stockage de la reference extrait de la table INSERTED
SELECT 
@refpiece= ref_piece
--En partant de la table Tempo_1 à laquelle je donne l'alias (T)
-- je joins la table INSERTED en établissant une relation d'ID.
FROM dbo.Tempo_1 T INNER JOIN inserted i ON T.id_enchere = i.id_enchere INNER JOIN  base_repartition.dbo.ENCHERES_History e2 ON  (e2.id_enchere= i.id_enchere)
INNER JOIN base_repartition.dbo.PIECES_History P1 ON (P1.id_piece=e2.id_piece)
 
 
--Compter le nombre d'ordres
SELECT
@numb=count(id_enchere) FROM (SELECT TOP 2
ref_piece,  e2.montant_enchere,id_enchere
FROM base_repartition.dbo.ENCHERES_History e2
INNER JOIN base_repartition.dbo.PIECES_History P1
ON (P1.id_piece=e2.id_piece)
WHERE ref_piece=@refpiece
GROUP BY ref_piece,e2.montant_enchere,id_enchere
ORDER BY e2.montant_enchere DESC) RESULT
 
--Si un seul ordre pour la référence
IF @numb=1
BEGIN
--Recuperer le prix de départ du lot
SELECT TOP 1
@prixdepart=P1.startprice_piece,
@reflot=ref_piece,
@idoffre=e2.id_enchere
FROM base_repartition.dbo.ENCHERES_History e2
INNER JOIN base_repartition.dbo.PIECES_History P1
ON (P1.id_piece=e2.id_piece)
WHERE ref_piece=@refpiece
GROUP BY ref_piece,e2.montant_enchere, e2.id_enchere, P1.startprice_piece
ORDER BY e2.montant_enchere DESC
 
--Mettre à jour la colonne montant_majore avec le prix de départ
-- Execute the INSERT statement.
UPDATE  base_repartition.dbo.ENCHERES_History
SET
montant_majore=@prixdepart
FROM base_repartition.dbo.ENCHERES_History
WHERE  base_repartition.dbo.ENCHERES_History.id_enchere=@idoffre
END
 
ELSE
BEGIN
--Si au moins deux ordres pour une référence
SELECT TOP 1
--Recuperer le montant de l'ordre tout de suite après enchère gagnante
@montant_avcom=e2.montant_enchere,
@reflot=ref_piece,
@idoffre=e2.id_enchere
FROM base_repartition.dbo.ENCHERES_History e2
INNER JOIN base_repartition.dbo.PIECES_History P1
ON (P1.id_piece=e2.id_piece)
Left join Tempo_1 T1
ON (T1.id_enchere=e2.id_enchere) 
WHERE ref_piece=@refpiece AND T1. id_enchere IS NULL
GROUP BY ref_piece,e2.montant_enchere, e2.id_enchere
ORDER BY e2.montant_enchere DESC
 
--Mettre à jour la colonne montant_majore avec la deuxième meilleur offre plus la commission
-- Execute the INSERT statement.
UPDATE  base_repartition.dbo.ENCHERES_History
SET
montant_majore=@montant_avcom+2
FROM base_repartition.dbo.ENCHERES_History INNER join Tempo_1 T1 ON (T1.id_enchere=base_repartition.dbo.ENCHERES_History.id_enchere) INNER JOIN base_repartition.dbo.PIECES_History P1
ON (P1.id_piece=base_repartition.dbo.ENCHERES_History.id_piece)
WHERE  P1.ref_piece=@reflot AND  base_repartition.dbo.ENCHERES_History.id_enchere=T1.id_enchere
END
BEGIN
COMMIT TRANSACTION Test
END