comment faire un script avec une boucle for en sql ?
Version imprimable
comment faire un script avec une boucle for en sql ?
Salut,
Il me semble que cela n'est pas possible en SQL pur, il faut l'associer à autre chose !
:?
en fait ce que je faire c'est ça :
J'ai une table avec 3 champs :
je veux copier le contenu de la valeur col3 pour FR dans IT de façon à avoir ce qui suit :Code:
1
2
3
4
5
6
7 col1 col2 col3 toto FR aaaaaaaaaaaaa toto DE bbbbbbbbbbbbb toto IT titi FR ffffffffffffff titi DE ddddddddddddd titi IT
je ne peux pas faire ceci ligne par ligne puisque j'ai 14000 lignes dans ma base de données.Code:
1
2
3 toto FR aaaaaaaaaaaaa toto DE bbbbbbbbbbbbb toto IT aaaaaaaaaaaaa
Tu peux le faire en une requête :
Code:
1
2
3
4
5
6
7
8 UPDATE matable AS it SET col3 = ( SELECT fr.col3 FROM matable AS fr WHERE fr.col2 = 'FR' AND fr.col1 = it.col1 ) WHERE it.col2 = 'IT' ;
j'ai fait ceci :
mais j'ai une erreur que je ne comprend pas qui est la suivanteCode:
1
2
3
4
5
6
7 UPDATE BUNDLE_WEB AS it SET BUNDLE_WEB.BUNDLE_VALUE = ( SELECT fr.BUNDLE_WEB.BUNDLE_VALUE FROM BUNDLE_WEB AS fr WHERE fr.BUNDLE_WEB.LANGUAGE = 'FR' AND fr.BUNDLE_WEB.BUNDLE_KEY = it.BUNDLE_WEB.BUNDLE_KEY ) WHERE it.BUNDLE_WEB.LANGUAGE = 'IT';
Incorrect syntax near '='.
Server Message: Number 107, Severity 15
Server 'SPARCLOPH02', Line 3:
The column prefix '.fr.BUNDLE_WEB.' does not match with a table name or alias name used in the query. Either the table is not specified in the FROM clause or it has a correlation name which must be used instead.
Ta requête doit être du style :
Code:
1
2
3
4
5
6
7 UPDATE BUNDLE_WEB AS it SET it.BUNDLE_VALUE = ( SELECT fr.BUNDLE_VALUE FROM BUNDLE_WEB AS fr WHERE fr.LANGUAGE = 'FR' AND fr.BUNDLE_KEY = it.BUNDLE_KEY ) WHERE it.LANGUAGE = 'IT';
ça ne marche pas non plus et j'ai ceci comme message
Incorrect syntax near '='.
Server Message: Number 107, Severity 15
Server 'SPARCLOPH02', Line 3:
The column prefix '.fr.' does not match with a table name or alias name used in the query. Either the table is not specified in the FROM clause or it has a correlation name which must be used instead.
Code:
1
2
3
4
5
6
7 UPDATE BUNDLE_WEB AS it SET it.BUNDLE_VALUE = ( SELECT fr.BUNDLE_VALUE FROM BUNDLE_WEB AS fr WHERE (fr.LANGUAGE = 'FR' AND fr.BUNDLE_KEY = it.BUNDLE_KEY) ) WHERE it.LANGUAGE = 'IT';
mais rien n'a changé dans ta requete, tu écris tj la meme chose
Si j'ai rajouté une parenthèse dans la fonction WHERE
oui mais ça fait la meme erreur
Par contre je ne sais pas si "LANGUAGE" n'est pas un mot clé, peux-tu le changer, et refaire tourner la requête.
non c'est le nom de ma colonne, je l'utilise depuis toujours sans pb, ce qu'il ne reconnait pas c'est le prefix fr (d'apres le message d'erreur)
j'ai mis ceci :
Code:
1
2
3
4
5
6
7 UPDATE BUNDLE_WEB AS it SET it.BUNDLE_VALUE = ( SELECT fr.BUNDLE_VALUE FROM BUNDLE_WEB AS fr WHERE (fr.BUNDLE_WEB.LANGUAGE = 'FR' AND fr.BUNDLE_KEY = it.BUNDLE_KEY) ) WHERE it.BUNDLE_WEB.LANGUAGE = 'IT'
Et sous cette forme ?
Code:
1
2
3
4
5
6
7 UPDATE BUNDLE_WEB SET BUNDLE_VALUE = ( SELECT fr.BUNDLE_VALUE FROM BUNDLE_WEB AS fr WHERE (fr.LANGUAGE = 'FR' AND fr.BUNDLE_KEY = BUNDLE_WEB.BUNDLE_KEY) ) WHERE BUNDLE_WEB.LANGUAGE = 'IT'
Code:
1
2
3
4
5
6 UPDATE BUNDLE_WEB AS table1 SET table1.BUNDLE_VALUE = ( SELECT table2.BUNDLE_VALUE FROM BUNDLE_WEB AS table2 WHERE (table2.LANGUAGE = 'FR' AND table2.BUNDLE_KEY = table1.BUNDLE_KEY) ) WHERE table1.LANGUAGE = 'IT'
c mieux merci