Précédent   Forum des professionnels en informatique > Bases de données > Oracle
Oracle Forum Oracle : le serveur, les outils, ... Voir F.A.Q Oracle Tutoriels Oracle
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 27/04/2011, 13h13   #1
Nouveau Membre du Club
 
Hazem Krichene
Inscription : novembre 2010
Messages : 99
Détails du profil
Informations personnelles :
Nom : Hazem Krichene

Informations forums :
Inscription : novembre 2010
Messages : 99
Points : 26
Points : 26
Par défaut Requête update à plusieurs sélections

Bonjour à tous,

J'ai deux tables: table testt formée de 4 colonnes (Libe, col1, col2, col3)
et une table testt_source formée de 3 colonnes (Libe, test1, test2).

Voici la table testt_source :
LIBE            TEST1      TEST2
---------- ---------- ----------
col1                1          2
col2                3          4
col3                7          8
Voici la table testt :
LIBE             COL1       COL2       COL3
---------- ---------- ---------- ----------
test1
test2
j'aimerai mettre à jour la table testt de sorte que la colonne col1 par exemple ait comme valeurs (1 , 2).

Pour ce faire j'ai exécuté la requête update suivante:

Code :
1
2
3
UPDATE testt SET 
  ((testt.col1 = (SELECT tt_sou.test1 FROM testt_source tt_sou WHERE tt_sou.libe = 'col1') WHERE testt.libe = 'test1') 
   AND (testt.col1 =(SELECT tt_sou.test2 FROM testt_source tt_sou WHERE  tt_sou.libe = 'col1') WHERE testt.libe = 'test2'))
Mais elle me renvoie l'erreur :

Citation:
ORA-01747 invalid user.table.column, table.column, or column specification
Est ce que je pourrais trouver de l'aide, svp

Merci.
hazem2410 est déconnecté   Envoyer un message privé Réponse avec citation 01
Vieux 27/04/2011, 14h57   #2
Modérateur
 
Homme Fabien
Ingénieur d'études en décisionnel
Inscription : septembre 2008
Messages : 5 684
Détails du profil
Informations personnelles :
Nom : Homme Fabien
Âge : 34
Localisation : France, Yvelines (Île de France)

Informations professionnelles :
Activité : Ingénieur d'études en décisionnel
Secteur : Arts - Culture

Informations forums :
Inscription : septembre 2008
Messages : 5 684
Points : 10 442
Points : 10 442
Envoyer un message via ICQ à Waldar Envoyer un message via Skype™ à Waldar
Votre syntaxe d'update ne représente rien.
Avez-vous cherché dans les tutoriels disponibles ?
__________________
Email : http://scr.im/waldar
Waldar est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 27/04/2011, 15h12   #3
Membre Expert
 
Inscription : août 2008
Messages : 1 271
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 1 271
Points : 1 929
Points : 1 929
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 merge INTO testt t
 USING (SELECT new_libe,
               max(case when libe = 'col1' then new_val end) AS col1,
               max(case when libe = 'col2' then new_val end) AS col2,
               max(case when libe = 'col3' then new_val end) AS col3
		  FROM (SELECT s.libe,
                       case when p.rn = 1 then 'test1'
                            when p.rn = 2 then 'test2'
                        end AS new_libe,
                       case when p.rn = 1 then s.test1
                            when p.rn = 2 then s.test2
                        end AS new_val
                  FROM testt_source s
                 CROSS JOIN (SELECT rownum AS rn FROM dual connect BY level <= 2) p
		       )
		 GROUP BY new_libe
		) u
    ON t.libe = u.new_libe
  when matched then
UPDATE SET t.col1 = u.col1,
       SET t.col2 = u.col2,
       SET t.col3 = u.col3
D'abord pivoter les colonnes en ligne le CROSS JOIN où 2 (dans connect by level <= 2) correspond aux nombres de colonnes de testt_source à pivoter.
Puis pivoter les lignes en colonnes (max(case when...)) pour que le select ressemble à la table testt.
Puis merge la requête dans testt.
Evidemment c'est assez statique car il faut hard coder les noms de colonnes.
Je n'ai pas testé la requête donc elle ne fonctionne peut être pas telle quelle mais je pense que le concept est valide.
skuatamad est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/04/2011, 20h20   #4
Membre Expert
 
Inscription : août 2008
Messages : 1 271
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 1 271
Points : 1 929
Points : 1 929
J'avais fais 2 erreurs de syntaxes, je reposte la requête correcte :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
 merge INTO testt t
 USING (SELECT new_libe,
               max(case when libe = 'col1' then new_val end) AS col1,
               max(case when libe = 'col2' then new_val end) AS col2,
               max(case when libe = 'col3' then new_val end) AS col3
          FROM (SELECT s.libe,
                       case when p.rn = 1 then 'test1'
                            when p.rn = 2 then 'test2'
                        end AS new_libe,
                       case when p.rn = 1 then s.test1
                            when p.rn = 2 then s.test2
                        end AS new_val
                  FROM testt_source s
                 CROSS JOIN (SELECT rownum AS rn FROM dual connect BY level <= 2) p
               )
         GROUP BY new_libe
       ) u
    ON (t.libe = u.new_libe)
  when matched then
UPDATE SET t.col1 = u.col1,
           t.col2 = u.col2,
           t.col3 = u.col3
et le jeu de test :
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
SQL> CREATE TABLE testt (Libe varchar2(10), col1 number, col2 number, col3 number)
  2  /
 
TABLE created.
 
SQL> CREATE TABLE testt_source (Libe varchar2(10), test1 number, test2 number)
  2  /
 
TABLE created.
 
SQL> INSERT INTO testt_source VALUES ('col1',1,2);
 
1 row created.
 
SQL> INSERT INTO testt_source VALUES ('col2',3,4);
 
1 row created.
 
SQL> INSERT INTO testt_source VALUES ('col3',7,8);
 
1 row created.
 
SQL> 
SQL> INSERT INTO testt (libe) VALUES ('test1');
 
1 row created.
 
SQL> INSERT INTO testt (libe) VALUES ('test2');
 
1 row created.
 
SQL> 
SQL> commit;
 
Commit complete.
 
SQL> 
SQL> SELECT * FROM testt_source;
 
LIBE            TEST1      TEST2
---------- ---------- ----------
col1                1          2
col2                3          4
col3                7          8
 
SQL> SELECT * FROM testt;
 
LIBE             COL1       COL2       COL3
---------- ---------- ---------- ----------
test1
test2
 
SQL> 
SQL>  merge INTO testt t
  2   USING (SELECT new_libe,
  3                 max(case when libe = 'col1' then new_val end) AS col1,
  4                 max(case when libe = 'col2' then new_val end) AS col2,
  5                 max(case when libe = 'col3' then new_val end) AS col3
  6            FROM (SELECT s.libe,
  7                         case when p.rn = 1 then 'test1'
  8                              when p.rn = 2 then 'test2'
  9                          end AS new_libe,
 10                         case when p.rn = 1 then s.test1
 11                              when p.rn = 2 then s.test2
 12                          end AS new_val
 13                    FROM testt_source s
 14                   CROSS JOIN (SELECT rownum AS rn FROM dual connect BY level <= 2) p
 15                 )
 16           GROUP BY new_libe
 17         ) u
 18      ON (t.libe = u.new_libe)
 19    when matched then
 20  UPDATE SET t.col1 = u.col1,
 21             t.col2 = u.col2,
 22             t.col3 = u.col3
 23  /
 
2 rows merged.
 
SQL> 
SQL> SELECT * FROM testt;
 
LIBE             COL1       COL2       COL3
---------- ---------- ---------- ----------
test1               1          3          7
test2               2          4          8
 
SQL>
skuatamad est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/04/2011, 12h02   #5
Nouveau Membre du Club
 
Hazem Krichene
Inscription : novembre 2010
Messages : 99
Détails du profil
Informations personnelles :
Nom : Hazem Krichene

Informations forums :
Inscription : novembre 2010
Messages : 99
Points : 26
Points : 26
Bonjour,

Merci énormément pour ces clarifications
J'ai adapté ton code à mon cas réel, voici le code

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
 
 
    merge INTO tb_gisement t 
     USING (SELECT new_libe,
                  max(case when DIM_ANALYSE = 'NBR DE CLIENT' then new_val end) AS NBR_CLT_EQ,
                  max(case when DIM_ANALYSE = 'CLIENTS CIBLE' then new_val end) AS POP_CIBLE_EQ,
                  max(case when DIM_ANALYSE = 'CIBLE NON EQUIPES' then new_val end) AS CIBLE_NN_EQ
             FROM (SELECT s.libe,
                          case when p.rn = 1 then 'SICAV'
                               when p.rn = 2 then 'TPE'
                               when p.rn = 3 then 'CARTE_AFF'
                               when p.rn = 4 then 'BIATNET'
                               when p.rn = 5 then 'MULTIVIR'
                               when p.rn = 6 then 'CARTE_SAL'
                               when p.rn = 7 then 'SD_CHANGE'
                               when p.rn = 8 then 'CPT_DEV'
                               when p.rn = 9 then 'AVA'
                               when p.rn = 10 then 'COMEX'
                               when p.rn = 10 then 'NEG_SDM'
                               when p.rn = 12 then 'BIATCAPITAL'
                               when p.rn = 13 then 'PROTECTRICE'
                               when p.rn = 14 then 'DAB'
                               when p.rn = 15 then 'CONVENTION'
                           end AS new_libe,
                          case when p.rn = 1 then s.SICAV
                               when p.rn = 2 then s.TPE
                               when p.rn = 3 then s.CARTE_AFF
                               when p.rn = 4 then s.BIATNET
                               when p.rn = 5 then s.MULTIVIR
                               when p.rn = 6 then s.CARTE_SAL
                               when p.rn = 7 then s.SD_CHANGE
                               when p.rn = 8 then s.CPT_DEV
                               when p.rn = 9 then s.AVA
                               when p.rn = 10 then s.COMEX
                               when p.rn = 11 then s.NEG_SDM
                               when p.rn = 12 then s.BIATCAPITAL
                               when p.rn = 13 then s.PROTECTRICE
                               when p.rn = 14 then s.DAB
                               when p.rn = 15 then s.CONVENTION
                         end AS new_val
                    FROM vue_gisement s
                   CROSS JOIN (SELECT rownum AS rn FROM dual connect BY level <= 15) p
                 )
           GROUP BY new_libe
          ) u
       ON (t.produits = u.new_libe)
    when matched then
   UPDATE SET t.SICAV = u.SICAV,
           t.TPE = u.TPE,
           t.CARTE_AFF = u.CARTE_AFF
           t.BIATNET = u.BIATNET
           t.MULTIVIR = u.MULTIVIR
           t.CARTE_SAL = u.CARTE_SAL
           t.SD_CHANGE = u.SD_CHANGE
           t.CPT_DEV = u.CPT_DEV
           t.AVA = u.AVA
           t.COMEX = u.COMEX
           t.NEG_SDM = u.NEG_SDM
           t.BIATCAPITAL = u.BIATCAPITAL
           t.PROTECTRICE = u.PROTECTRICE
           t.DAB = u.DAB
           t.CONVENTION = u.CONVENTION
Mais il me renvoie l'erreur suivante :


ORA-00933 SQL command not properly ended
hazem2410 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/04/2011, 14h34   #6
Membre Expert
 
Inscription : août 2008
Messages : 1 271
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 1 271
Points : 1 929
Points : 1 929
Normalement tu essaies d'update les colonnes NBR_CLT_EQ, POP_CIBLE_EQ, CIBLE_NN_EQ de tb_gisement donc:
Code :
1
2
3
UPDATE SET t.NBR_CLT_EQ = u.NBR_CLT_EQ,
           t.POP_CIBLE_EQ = u.POP_CIBLE_EQ,
           t.CIBLE_NN_EQ = u.CIBLE_NN_EQ
Si ça n'est pas le cas il faudra réexpliquer le besoin.
skuatamad est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/04/2011, 15h43   #7
Nouveau Membre du Club
 
Hazem Krichene
Inscription : novembre 2010
Messages : 99
Détails du profil
Informations personnelles :
Nom : Hazem Krichene

Informations forums :
Inscription : novembre 2010
Messages : 99
Points : 26
Points : 26
Citation:
Envoyé par skuatamad Voir le message
Normalement tu essaies d'update les colonnes NBR_CLT_EQ, POP_CIBLE_EQ, CIBLE_NN_EQ de tb_gisement donc:
Code :
1
2
3
UPDATE SET t.NBR_CLT_EQ = u.NBR_CLT_EQ,
           t.POP_CIBLE_EQ = u.POP_CIBLE_EQ,
           t.CIBLE_NN_EQ = u.CIBLE_NN_EQ
Si ça n'est pas le cas il faudra réexpliquer le besoin.
Merci infiniment pour ta coopération, tu m'as sauvé la vie
hazem2410 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 08h14.


 
 
 
 
Partenaires

Hébergement Web