Bonjour à tous,
Je viens vers vous car je n'arrive pas à solutionner mon problème!
En fait j'ai 2 tables, une table contenant un ensemble d'appels telephoniques et
dans laquelle se fait des update sur le statut de l'appel et une autre contenant l'historique sur les changements de statuts.
Voici la structure de mes 2 tables:
Clef primaire sur l'identifiant Id_AppelsHisto
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 USE [MessageService] GO /****** Object: Table [dbo].[Appels] Script Date: 07/01/2010 09:02:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO TABLE [dbo].[Appels]( [Id_Appels] [int] IDENTITY(1,1) NOT NULL, [DateIntegration] [nvarchar](10) NULL, [HeureIntegration] [nvarchar](4) NULL, [FK_Campagnes] [int] NOT NULL, [FK_Prestataires] [int] NULL, [TelAppelant] [nvarchar](15) NULL, [Tel] [nvarchar](15) NULL, [CodeMessagesClients] [nvarchar](5) NOT NULL, [MessageClient] [ntext] NULL, [Id_Statuts] [int] NOT NULL, [LibelleClientsStatuts] [nvarchar](50) NOT NULL, [NombreMessage] [int] NOT NULL, [SVI_Indice] [int] NOT NULL, [SVI_Table] [nvarchar](50) NULL, [NombreEssai] [int] NOT NULL, [DateHeureStatut] [datetime] NOT NULL, [NomFichier] [nvarchar](50) NULL, [LigneFichier] [nvarchar](500) NULL, [TraitementEnCours] [bit] NOT NULL, [DF_Appels_Traitement] DEFAULT ((0)), [Push_Id] [nvarchar](40) NULL, [DF_Appels_Push_Id] DEFAULT (N'NC') ) Clef primaire sur l'identifiant Id_appels Et la table historique: TABLE [dbo].[Histo_Appels]( [Id_AppelsHisto] [int] IDENTITY(1,1) NOT NULL, [FK_Appels] [int] NOT NULL, [DateAppels] [datetime] NOT NULL, [HeureAppels] [nvarchar](4) NULL, [DateHisto] [datetime] NOT NULL, [FK_Campagnes] [int] NULL, [LibelleCampagnes] [nvarchar](50) NULL, [ModeCampagnes] [nvarchar](50) NULL, [TelAppelantCampagnes] [nvarchar](15) NULL, [FK_Clients] [int] NULL, [NomClients] [nvarchar](50) NULL, [FK_Prestataires] [int] NULL, [NomPrestataires] [nvarchar](50) NULL, [FK_PrestatairesDefinition] [int] NULL, [NomPrestatairesDefinition] [nvarchar](50) NULL, [DateIntegration] [datetime] NULL, [StatutIntegration] [int] NULL, [LibelleStatutIntegration] [nvarchar](50) NULL, [Tel] [nvarchar](15) NULL, [MessageClient] [ntext] NULL, [NombreMessage] [int] NULL, [SVI_Indice] [int] NULL, [SVI_Table] [nvarchar](50) NULL, [NombreEssai] [int] NULL, [NomFichier] [nvarchar](50) NULL, [LigneFichier] [nvarchar](500) NULL, [Push_Id] [nvarchar](40) NULL )
Je souhaiterai insérer dans la table Histo_Appels toutes les lignes qui font l'objet d'un changement de statut (StatutIntegration) dans la table Appels
(Il 'y a pas de suppression de lignes dans la table Appels)
Voici mon trigger:
Probleme:
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 TRIGGER [dbo].[AppelsHisto] ON [dbo].[Appels] AFTER INSERT, UPDATE AS IF (UPDATE (Id_Statuts)) --Test si la colonne Id_Statuts est modifie BEGIN DECLARE @Id_Appels int DECLARE @FK_Campagnes int DECLARE @DateIntegration nvarchar(10) DECLARE @HeureIntegration nvarchar(4) DECLARE @StatutIntegration int DECLARE @Tel nvarchar(15) DECLARE @NombreMessage int DECLARE @SVI_Indice int DECLARE @SVI_Table nvarchar(50) DECLARE @NombreEssai int DECLARE @NomFichier nvarchar(50) DECLARE @LigneFichier nvarchar(500) DECLARE @Push_Id nvarchar(40) DECLARE @TraitementEnCours bit SELECT @Id_Appels = Id_Appels, @FK_Campagnes = FK_Campagnes, @DateIntegration = DateIntegration, @HeureIntegration = HeureIntegration, @StatutIntegration = Id_Statuts, @Tel = Tel, @NombreMessage = NombreMessage, @SVI_Indice = SVI_Indice, @SVI_Table = SVI_Table, @NombreEssai = NombreEssai, @NomFichier = NomFichier, @LigneFichier = LigneFichier, @Push_Id = Push_Id, @TraitementEnCours = TraitementEnCours FROM INSERTED DECLARE @MessageClient nvarchar(4000) DECLARE @LibelleCampagnes nvarchar(50) DECLARE @ModeCampagnes nvarchar(50) DECLARE @TelAppelantCampagnes nvarchar(15) DECLARE @FK_Clients int DECLARE @NomClient nvarchar(50) DECLARE @LibelleStatutIntegration nvarchar(50) DECLARE @FK_Prestataires int DECLARE @NomPrestataires nvarchar(50) DECLARE @FK_PrestatairesDefinition int -- Infos appels SELECT @MessageClient = MessageClient FROM Appels WHERE Id_Appels = @Id_Appels -- Infos campagnes SELECT @LibelleCampagnes = Nom, @ModeCampagnes = Mode, @TelAppelantCampagnes = TelAppelant, @FK_Clients = Fk_Clients, @FK_Prestataires = Fk_Prestataires FROM Campagnes WHERE Id_Campagnes = @FK_Campagnes -- Infos Clients SELECT @NomClient = Nom FROM Clients WHERE ID_Clients = @FK_Clients -- Infos statuts SELECT @LibelleStatutIntegration = Libelle FROM clientsStatuts WHERE FK_clients = @FK_Clients AND CodeStatuts = @StatutIntegration -- Infos Prestataires SELECT @NomPrestataires = Nom, @FK_PrestatairesDefinition = FK_PrestatairesDefinition FROM Prestataires WHERE Id_Prestataires = @FK_Prestataires -- Insertion table historique sauf update sur le flag traitement en cours IF @TraitementEnCours = 0 BEGIN INSERT INTO Histo_Appels (FK_appels, DateAppels, datehisto, fk_campagnes, libellecampagnes, modecampagnes, TelAppelantCampagnes, FK_Clients, NomClients, FK_Prestataires, NomPrestataires, FK_PrestatairesDefinition, DateIntegration, StatutIntegration, LibelleStatutIntegration, Tel, MessageClient, NombreMessage, SVI_Indice, SVI_Table, NombreEssai, NomFichier, LigneFichier, Push_Id) VALUES (@Id_Appels, @DateIntegration, getdate(), @FK_Campagnes, @LibelleCampagnes, @ModeCampagnes, @TelAppelantCampagnes, @FK_Clients, @NomClient, @FK_Prestataires, @NomPrestataires, @FK_PrestatairesDefinition, convert(datetime, @DateIntegration + ' ' + substring(@HeureIntegration,1,2) + ':' + substring(@HeureIntegration,4,2), 108), @StatutIntegration, @LibelleStatutIntegration, @Tel, @MessageClient, @NombreMessage, @SVI_Indice, @SVI_Table, @NombreEssai, @NomFichier, @LigneFichier, @Push_Id) END END
En fait j'ai 2 process Webservice qui viennent 1) inserer les nouvelles lignes dans la table d'appels et 2) celui qui vient mettre a jour les statuts.
Or il s'avere que j'ai des doublons de lignes dans la table historiques!!!
J'ai en effet plusieurs fois le même StatutIntegration sur des lignes differentes evidemmenent.
Pourquoi un tel phénomène?
Quand j'essaye manuellement le trigger semble fonctionner mais une fois dans son contexte je n'ai pas le résultat attendu!
D'avance merci pour votre aide sur mon soucis.
A+
lesapo
PS: Il s'agit de mon premier trigger donc...
Partager