Bonjour à Tous,

Suite à un soucis sur une base de données j'ai mis en place un cursor
qui m'insere dans une table tmp des numéros que je doit decoder pour retrouver des valeurs decoder par rapport une startdeate et endDate qu'ensuite je dois comparer avec une autre table mais le soucis que je rencontre et comme je fais un select count avec des états différents je m'apercois qui y a des doublons et que la sum du count du select 2 et 4 ne correspond pas au count du select 1 bien entendu que le numeros sont uniques j ai verifier si il y avait des doublons voici la partie du code en question si quelqu'un aurais une idé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
------------------------------------------------------------------------------------------------------------
DECLARE @valeur_a_decoder VARCHAR(100)
DECLARE @critere1 VARCHAR(100)
DECLARE @critere2 INT
DECLARE @critere3 INT
DECLARE @total_numeros_etat1_table2 INT
DECLARE @total_numeros_etat1_table INT
DECLARE @total_numeros_etat1_table_tmp INT
DECLARE @total_numeros_etat2_table INT
DECLARE @total_numeros_etat2_table_tmp INT
DECLARE @total_numeros1_etat1_table INT
DECLARE @total_numeros1_etat1_table_tmp INT
DECLARE @total_numeros1_etat2_table INT
DECLARE @total_numeros1_etat2_table_tmp INT
DECLARE @Start_Period_Date	VARCHAR(300)
DECLARE @End_Period_Date	VARCHAR(300)
 
 
SET @Start_Period_Date = '20090101000000'
SET @End_Period_Date   = '20090803235959'
 
SELECT champs_a_decoder INTO #champs_a_decoder FROM table WHERE champs1 = 'status' and operation_date >= @Start_Period_Date and operation_date <= @End_Period_Date and error_text = 'status' 
/*------------------------------------ SET TEMPORARY VARIABLES ------------------------------------------------------------------------------------------------*/
SET @total_numeros_etat1_table_tmp = 0
SET @total_numeros_etat2_table_tmp = 0
SET @total_numeros1_etat1_table_tmp = 0
SET @total_numeros1_etat2_table_tmp = 0
 
DECLARE champs_a_decoder_cursor SCROLL CURSOR 
FOR SELECT *  from #champs_a_decoder 
OPEN champs_a_decoder_cursor
FETCH NEXT FROM champs_a_decoder_cursor INTO @valeur_a_decoder
WHILE @@fetch_status = 0
BEGIN
 
SET @critere1 = substring(@valeur_a_decoder,7,1)
SET @critere2 = substring(@valeur_a_decoder,8,3)
SET @critere3 = substring(@valeur_a_decoder,11,5)
IF @critere1 = 1
	BEGIN
		SET @critere1 = 'XXXXX'
END
ELSE
	BEGIN
		SET @critere1 = 'YYYYY'
END
 
/* ---------------------------------------------------------SELECT 1 ---------------------------------------------------------------------------*/
SELECT @total_numeros_etat1_table2 = COUNT(*) FROM table2 WHERE champ1 = @critere1 and champ2 = @critere2 and champ3 = @critere3 and state = 'etat1'
IF @total_numeros_etat1_table2 = 1
BEGIN
 
		SET @total_numeros_etat1_table2_tmp = @total_numeros_etat1_table2_tmp + 1
END
/* ---------------------------------------------------------SELECT 2 ---------------------------------------------------------------------------*/
SELECT @total_numeros_etat1_table = COUNT(*) FROM table2 WHERE champ1 = 'YYYYY' and champ2 = @critere2 and champ3 = @critere3 and state = 'etat1'
IF @total_numeros_etat1_table_tmp = 1
BEGIN
 
		SET @total_numeros_etat1_table_tmp = @total_numeros_etat1_table_tmp + 1
END
/* ---------------------------------------------------------SELECT 3 ---------------------------------------------------------------------------*/
SELECT @total_numeros_etat2_table = COUNT(*) FROM table2 WHERE champ1 = @critere1 and champ2 = @critere2 and champ3 = @critere3 and state = 'etat2'
IF @total_numeros_etat2_table = 1
	BEGIN
		SET @total_numeros_etat2_table_tmp = @total_numeros_etat2_table_tmp + 1
END
/* ---------------------------------------------------------SELECT 4 ---------------------------------------------------------------------------*/
SELECT DECLARE @total_numeros1_etat1_table = COUNT(*) FROM table2 WHERE champ1 = 'XXXXX' and champ2 = @critere2 and champ3 = @critere3 and state = 'etat1'
IF DECLARE @total_numeros1_etat1_table = 1
	BEGIN
		SET DECLARE @total_numeros1_etat1_table_tmp = DECLARE @total_numeros1_etat1_table_tmp + 1
 
END
/* ---------------------------------------------------------SELECT 5 ---------------------------------------------------------------------------*/  
SELECT @total_numeros1_etat2_table = COUNT(*) FROM table2 WHERE champ1 = @critere1 and champ2 = @critere2 and champ3 = @critere3 and state = 'etat2'
 
IF @total_numeros1_etat2_table = 1 
  BEGIN
 
      SET @total_numeros1_etat2_table_tmp = @total_numeros1_etat2_table_tmp + 1 
 
  END
FETCH NEXT FROM champs_a_decoder_cursor INTO @valeur_a_decoder
END
CLOSE champs_a_decoder_cursor
DEALLOCATE champs_a_decoder_cursor
PRINT  '-------------- End -------------'

Merci