Bonne rencontre,

Je me casse les dents sur une query alors je viens chercher l’inspiration auprès de vous.

Voici le cas :

J’ai une table customers_category (Idcustomer,idcategory).
je reçois des fichiers xml que je stocke dans la base de données et je fais ensuite des query sur le champ xml.

Le fichier contient par exemple :

idcustomer 55 avec idcategory 4, idcategory 7 mais aucune entrée sur l’idcustomer 100

La table customers_category contient par exemple :

idcustomer 55, idcategory 4
idcustomer 55, idcategory 10
idcustomer 100, idcategory 1
idcustomer 100, idcategory 2


J’ai essayé de travailler avec du MERGE, pour l’insert et l’update pas de problème.

Une fois que j’ajoute le DELETE, il me vide complètement ma table customers_category. Ma condition de filtre n’est pas bonne mais je ne sais pas si j’ai les valeurs nécessaires pour y arriver.

Voici le code procédure stockée.

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
USE [ZoneCustomerExchange]
GO
/****** Object:  StoredProcedure [dbo].[SetDataCustomersCategory]    Script Date: 08-06-17 10:47:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Description: Permet d'ajouter des catégories à un client
-- =============================================
ALTER PROCEDURE [dbo].[SetDataCustomersCategory]
	-- Add the parameters for the stored procedure here
 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
 
   	DECLARE @idFile int
	DECLARE @NameFile varchar(50)
 
	--Customers
	DECLARE XmlData CURSOR FOR 
						SELECT  
							[dbo].[FilesXML].IDFilesXML,
							[dbo].[FilesXML].NameFile   
						FROM [dbo].[FilesXML]
						WHERE 
							[dbo].[FilesXML].Parite = 1 
						AND [dbo].[FilesXML].FKSource = 1
						AND [dbo].[FilesXML].DateExplore IS NULL
						AND [dbo].[FilesXML].Type = 'CatCustomers'			
						ORDER BY [dbo].[FilesXML].IDFilesXML
   OPEN XmlData
   FETCH XmlData INTO @idFile,@NameFile   
 
 
   WHILE @@FETCH_STATUS = 0
	BEGIN
 
		MERGE [dbo].[customers_category] AS target
			USING (
				SELECT
				  pref.value('(email/text())[1]', 'varchar(255)') as email,
				  sref.value('(text())[1]', 'varchar(50)') as CAT,
				  @NameFile as NameFile,
				  1 as IDSource,
				  [dbo].[customers].[idcustomer],
				  [dbo].[customers].[IDSourceIdAgency],
				  [dbo].[category].idcategory
				FROM  
					FilesXML CROSS APPLY
					XmlData.nodes('/Root/customer') AS customer(pref)
					CROSS APPLY
					 pref.nodes('category/idcategory') AS CAT(sref)
				INNER JOIN [dbo].[customers]
				ON [dbo].[customers].email =  pref.value('(email/text())[1]', 'varchar(255)')   
				INNER JOIN [dbo].[category]
				ON [dbo].[category].IDSourceIdCategory = sref.value('(text())[1]', 'varchar(50)')  AND [dbo].[category].IDSourceIdAgency = [dbo].[customers].[IDSourceIdAgency]
				WHERE 
					FilesXML.IDFilesXML = @idFile
				AND pref.value('(email/text())[1]', 'varchar(255)')  <> 'NULL'
				AND  sref.value('(text())[1]', 'varchar(50)')   <> 'NULL'
			) as Source
			ON (target.[Idcategory] = Source.idcategory AND target.Idcustomer =  Source.idcustomer)
		WHEN MATCHED THEN
 
			 UPDATE SET
				target.NameFile = Source.NameFile
 
		WHEN NOT MATCHED BY target THEN 
 
	    		INSERT 	(Idcategory,Idcustomer,NameFile	)
				VALUES (
				 Source.idcategory
				,Source.idcustomer
				,Source.NameFile
				)
 
		WHEN NOT MATCHED AND target.Idcustomer =  Source.idcustomer THEN		
				DELETE
 
		OUTPUT $action, inserted.*, deleted.*;
 
		FETCH XmlData INTO @idFile, @NameFile
	END
   CLOSE XmlData
   DEALLOCATE XmlData
 
 
 
 
END
Merci de votre aide.
Raphaël.