Précédent   Forum des professionnels en informatique > Bases de données > DB2
DB2 Forum d'entraide technique sur la base de données DB2. Voir aussi -> Rubrique DB2
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 21/09/2007, 18h03   #1
Membre à l'essai
 
Inscription : septembre 2006
Messages : 115
Détails du profil
Informations forums :
Inscription : septembre 2006
Messages : 115
Points : 23
Points : 23
Par défaut comment utiliser le 'CASE' statement

Bonjour a vous,

De l'aide!!!!
voici mon probleme, dès que je décommente... enlever le signe du commentaire --) pour l'ajouter au select par exemple la colonne (-- M.fsp_id, )
, j'obtient une erreur :
Code :
1
2
3
CONVERT    SQLPRINT              32  RETURN CODE IS 8                                                                                                                                                                                                                                                                                 
CONVERT    SQLTERM                1 DSNH016I E     DSNHPARS LINE 28 COL 22  "END" REQUIRED                                                                                                                                                                                                               
CONVERT    SQLTERM                2 DSNH104I E     DSNHPARS LINE 28 COL 22  ILLEGAL SYMBOL ",". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: END
par contre comme ça, ça marche bien,
Code :
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
 
CREATE PROCEDURE FSMS.STATUS_LIST_TEST (     IN I_language       character(2),
                                        IN I_parameter      character(7),
                                        out O_error_nr      integer )
 
    RESULT SETS 1
    LANGUAGE SQL
--    WLM ENVIRONMENT DB2DWAPP
    MODIFIES SQL DATA
    COLLID FSMS
    asutime no LIMIT
    RUN OPTIONS 'NOTEST(ALL,*,,VADTCPIP&137.122.105.233:*)'
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
 
    DECLARE v_error INT DEFAULT 0;
 
    DECLARE cursor1 CURSOR WITH RETURN FOR
 
    SELECT  DISTINCT   M.sector_id,
    case  upper(I_parameter)
    when  'SECTORALL'  then
 
            M.type_cd
--            M.fsp_id,
--	    M.status_id,
--	    M.english_Html
--	    M.french_Html,
--	    M.update_userid,
--	    S.english_desc,
--	    S.french_desc,
--	    case upper(I_language)
--               when 'FR' then M.French_Html
--               else  M.English_Html
--            end Descrip_txt,
--
--            case upper(I_language)
--               when 'FR' then S.French_desc
--               else  S.English_desc
--            end Descrip_status
---
     when  'SECTOR'  then
             M.sector_id
     when 'FSPFSP'   then
             M.fsp_id
     when 'TYPEFS' then
             M.type_cd
     when 'STATUS' then
            S.french_desc
---            M.status_id
--            case upper(I_language)
--               when 'FR' then S.French_desc
--               else  S.English_desc
--            end Descrip_status
 
    end
 
    FROM    GAF.MFP_STATUS M
    INNER JOIN GAF.STATUS  S
	    ON M.Status_ID = S.Status_ID
--    ORDER BY M.sector_id,
--	    M.type_cd,
--	    M.fsp_id
--	    Descrip_status
    WITH UR;
 
    SET O_error_nr = 0;
 
--  open cursor1
    OPEN Cursor1;
 
END P1
 
 
 
-- --------------------------------------------------------------------
-- date         sr#     user
-- -----------  ----    -------- --------------------------------------
-- 2007-07-16           
-- Initial creation of stored procedure
-- ----------------------------------------------
machipot est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/09/2007, 02h11   #2
Membre Expert
 
Inscription : novembre 2004
Messages : 1 298
Détails du profil
Informations forums :
Inscription : novembre 2004
Messages : 1 298
Points : 1 355
Points : 1 355
Evidemment ! la colonne baptisée "Descrip_txt" ne peut pas prendre à la fois la valeur de la colonne M.type_cd et celle de la colonne M.fsp_id par exemple.
pour mieux comprendre, disons qu'un CASE peut se lire comme un IF :
IF upper(I_parameter) = 'SECTORALL' then Descrip_txt = M.type_cd
Mercure est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/09/2007, 14h16   #3
Membre à l'essai
 
Inscription : septembre 2006
Messages : 115
Détails du profil
Informations forums :
Inscription : septembre 2006
Messages : 115
Points : 23
Points : 23
Par défaut comment utiliser le 'CASE' statement

Merci Mercure, c'etait la fonctionalité du CASE qui m'avait échapé.....
ce que je veux est du genre:
si tu arrives a me restructurer ce code, tu me sauveras ben du temps.
A+

Code :
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
 
CREATE PROCEDURE FSMS.STATUS_LIST_TEST (     IN I_language       character(2),
                                        IN I_parameter      character(7),
                                        out O_error_nr      integer )
 
    RESULT SETS 1
    LANGUAGE SQL
--    WLM ENVIRONMENT DB2DWAPP
    MODIFIES SQL DATA
    COLLID FSMS
    asutime no LIMIT
    RUN OPTIONS 'NOTEST(ALL,*,,VADTCPIP&137.122.105.233:*)'
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
 
    DECLARE v_error INT DEFAULT 0;
 
    DECLARE cursor1 CURSOR WITH RETURN FOR
 
    SELECT  DISTINCT   M.sector_id,
    si  upper(I_parameter) est =  'SECTORALL'  alors je selectionne les champs suivants:
            SELECT  DISTINCT   M.sector_id,
            M.type_cd
            M.fsp_id,
	    M.status_id,
	    M.english_Html
	    M.french_Html,
	    M.update_userid,
	    S.english_desc,
	    S.french_desc,
	    case upper(I_language)
               when 'FR' then M.French_Html
               else  M.English_Html
            end Descrip_txt,
 
            case upper(I_language)
               when 'FR' then S.French_desc
               else  S.English_desc
            end Descrip_status
 
            ORDER BY M.sector_id,
            M.type_cd,
	M.fsp_id
	Descrip_status
 
 
 
     si  upper(I_parameter) est =  'SECTOR'  alors je selectionne les champs suivants:
             SELECT  DISTINCT   
             M.sector_id
             ORDER BY M.sector_id
 
     si  upper(I_parameter) est =  'FSPFSP'  alors je selectionne les champs suivants:
              SELECT  DISTINCT   
               M.fsp_id
              ORDER BY M.fsp_id
 
     si  upper(I_parameter) est =  'TYPEFS'  alors je selectionne les champs suivants:
             SELECT  DISTINCT   
             M.type_cd
             ORDER BY M.type_cd
 
     si  upper(I_parameter) ets =  'STATUS'  alors je selectionne les champs suivants:
            SELECT  DISTINCT   
             M.status_id
            case upper(I_language)
               when 'FR' then S.French_desc
               else  S.English_desc
            end Descrip_status
            ORDER BY Descrip_status
 
 
    FROM    GAF.MFP_STATUS M
    INNER JOIN GAF.STATUS  S
	    ON M.Status_ID = S.Status_ID
 
    WITH UR;
 
    SET O_error_nr = 0;
 
--  open cursor1
    OPEN Cursor1;
 
END P1
 
 
 
-- --------------------------------------------------------------------
-- date         sr#     user
-- -----------  ----    -------- --------------------------------------
-- 2007-07-16           
-- Initial creation of stored procedure
-- ----------------------------------------------
machipot est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 04h03.


 
 
 
 
Partenaires

Hébergement Web