Bonjour,

Dans le cadre d'une migration de données, j'ai besoin de créer plusieurs enregistrements dans une table, dont la clé n'est pas en auto-increment, et la valeur de cet identifiant est du VARCHAR.

Je prends le max() du champ à incrémenter, récupère la partie chiffre, que je transforme en integer, et que j'augmente de 1.
Ceci fonctionne, mais uniquement sur la première requête, car le max est récupérer une seule fois au début de la requête.
Comme cette requête est censé créer qq milliers de lignes en une seule exécution, il faudrait un genre de compteur d'insert.

Je ne peux modifier la structure de la table.

je suis sous Mysql.

Merci par avance.

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
insert into prestation (identifiant_prestation, fk_type_prestation_version, fk_etat_prestation, fk_contrat, no_commande_ft, no_ligne_commande, 
						code_offre_composee, code_offre, date_ar, date_creation, date_mes_technique, date_mes_commercial, date_maj, utilisateur_maj, 
						date_mes_retenue, date_contractuelle_opus, indicateur_resiliation, date_facturation_fas, date_declenchement_facturation, 
						contractant_representant, contractant_rue, contractant_code_postal, contractant_localite, contractant_telephone, contractant_telecopie,
						installation_code_postal, installation_localite, installation_telephone_site, installation_telecopie, version_jpa)

 select CONCAT('COL0',CAST(((cast(substring((select max(identifiant_prestation) as p from prestation), 4) AS SIGNED INTEGER))+ 1) AS CHAR)),
		(select tpv3.id
		from type_prestation_version tpv3, type_prestation tp3, type_ligne_commande tc3, type_ligne_commande_version tcv3
		where tp3.code = 'GCBLO_PE_FTVX'
		and tc3.code = 'GCBLO_STR'
		and tcv3.version_commande = 'V2'
		and tpv3.fk_type_prestation = tp3.id
		and tpv3.fk_type_ligne_commande_version = tcv3.id
		and tcv3.fk_type_ligne_commande = tc3.id),
		3, p.fk_contrat, ec.no_commande_ft, 1, ec.code_offre_composee, tc.fk_offre, 
		p.date_ar, mg.date_migration, mg.date_migration, mg.date_migration, mg.date_migration,  'migration', mg.date_migration, p.date_contractuelle_opus, 
		0, p.date_facturation_fas, p.date_declenchement_facturation, p.contractant_representant, p.contractant_rue, p.contractant_code_postal, p.contractant_localite,
		p.contractant_telephone, p.contractant_telecopie, p.installation_code_postal, p.installation_localite, p.installation_telephone_site, installation_telecopie, 0
from entete_commande ec, ligne_commande lc, migration_gcblo mg, ligne_prestation lp, prestation p, type_prestation_version tpv, type_ligne_commande_version tcv, type_ligne_commande tc, type_prestation tp
where ec.id = mg.fk_entete_commande
and mg.indicateur_migration_realisee = 0
and mg.indicateur_rejet = 0
and mg.code_operation = 'CSTR'
and mg.fk_operateur = (select id from operateur where operateur_siren = 488095803)
and lc.fk_entete_commande = ec.id
and lc.id = lp.fk_ligne_commande
and p.identifiant_prestation = lp.fk_prestation
and tpv.id = p.fk_type_prestation_version
and tcv.id = tpv.fk_type_ligne_commande_version
and tc.id = tcv.fk_type_ligne_commande
and tc.code = 'GCBLO_STR'
and tcv.version_commande = 'V2'
and tp.id = tpv.fk_type_prestation
and tp.code = 'GCBLO_PE_STR'
;