vico8000
Messages postés287Date d'inscriptionlundi 18 janvier 2016StatutMembreDernière intervention14 février 2018
-
12 juil. 2017 à 14:56
vico8000
Messages postés287Date d'inscriptionlundi 18 janvier 2016StatutMembreDernière intervention14 février 2018
-
17 juil. 2017 à 08:01
Bonjour,
(Nouveau problème Cirec :( )
Le problème du jour est un mélange de requête SQL.
Je m'explique. J'ai développé un soft de conversion pour passer mes données enregistrées sur des fichiers DBase vers un SGBD (MSSQL). Le soft de conversion marche parfaitement pour la plupart de mes fichiers.
Lorsque je m'attaque à des fichiers plus volumineux, ça commence à coincer. J'ai des fichiers avec environ 300 colonnes. pour la création de mes colonnes et de ma table, je procède de la façon suivante :
CreationTable:=False; setlength(Transtypage, 200); setlength(TabIndex, 20); NewChamp := 0; CommandeSQL := 'if not exists (Select * from sysobjects where name=''' + Nom_Fichier + ''' and xtype=''U'') ' ; CommandeSQL := CommandeSQL + 'Create table [' +Nom_Fichier + '] ('; Table_en_cours.TableName := Nom_Fichier; Table_en_cours.DatabaseName := Nom_Fichier; if active_Table(Table_en_cours, Emplacement , Nom_Fichier, '') then Begin for I := 0 to Table_en_cours.IndexDefs.Count -1 do Begin TabIndex[i]:=Table_en_cours.IndexDefs[i].Name; End; i := 0; if erreur = 4 then Begin CommandeSQL := ' if exists ( select * from sys.tables where name = N''' + nom_fichier + ''') DROP TABLE [' + nom_fichier + '] '; CommandeSQL := CommandeSQL + ' Create Table [' + Nom_fichier + '] ('; End; While i < Table_en_cours.FieldCount do Begin Transtypage[i] := FieldTypeAsString(Table_en_cours.Fields[i].DataType); if Transtypage[i] = 'ftString' then Transtypage[i] := 'Char' else if Transtypage[i] = 'ftDate' then Transtypage[i] := 'Date' else if Transtypage[i] = 'ftSmallint' then Transtypage[i] := 'Integer' else if Transtypage[i] = 'ftFloat' then Transtypage[i] := 'Float';
for j := 0 to 20 do Begin if TabIndex[j] = Table_en_cours.Fields[i].FieldName then Index := true ; End;
if CreationTable=false then Begin if Index = true then begin if (Transtypage[i] = 'Date') or (Transtypage[i] = 'Integer') then CommandeSQL := CommandeSQL + Table_en_cours.Fields[i].FieldName + ' ' + Transtypage[i] + ' NOT NULL;' else CommandeSQL := CommandeSQL + Table_en_cours.Fields[i].FieldName + ' ' + Transtypage[i] + '(' + inttostr(Table_en_cours.Fields[i].DataSize)+ ') NOT NULL);'; if Table_en_cours.Fields[i].FieldName = 'DATE' then Begin Inc(NewChamp, 1); PosDate := i; End; if Table_en_cours.Fields[i].FieldName = 'HEURE' then Begin Inc(NewChamp, 1); PosHeure := i; End; end else begin if (Transtypage[i] = 'Date') or (Transtypage[i] = 'Integer') then CommandeSQL := CommandeSQL + Table_en_cours.Fields[i].FieldName + ' ' + Transtypage[i] + ';' else CommandeSQL := CommandeSQL + Table_en_cours.Fields[i].FieldName + ' (' + inttostr(Table_en_cours.Fields[i].DataSize)+ ');' ; if Table_en_cours.Fields[i].FieldName = 'DATE' then Begin Inc(NewChamp, 1); PosDate := i; End; if Table_en_cours.Fields[i].FieldName = 'HEURE' then Begin Inc(NewChamp, 1); PosHeure := i; End; end; CreationTable := true; End else Begin if Index = true then begin if (Transtypage[i] = 'Date') or (Transtypage[i] = 'Integer') then CommandeSQL := 'if not exists (Select Null from information_schema.columns where TABLE_NAME = ''' + Nom_Fichier + ''' and COLUMN_NAME = ''' + Table_en_cours.Fields[i].FieldName + ''') ALTER TABLE ' + Nom_Fichier + ' ADD ' + Table_en_cours.Fields[i].FieldName + ' ' + Transtypage[i] + ' NOT NULL;' else CommandeSQL := 'if not exists (Select Null from information_schema.columns where TABLE_NAME = ''' + Nom_Fichier + ''' and COLUMN_NAME = ''' + Table_en_cours.Fields[i].FieldName + ''') ALTER TABLE ' + Nom_Fichier + ' ADD ' + Table_en_cours.Fields[i].FieldName + ' ' + Transtypage[i] + '(' + inttostr(Table_en_cours.Fields[i].DataSize) + ') NOT NULL;'; if Table_en_cours.Fields[i].FieldName = 'DATE' then Begin Inc(NewChamp, 1); PosDate := i; End; if Table_en_cours.Fields[i].FieldName = 'HEURE' then Begin Inc(NewChamp, 1); PosHeure := i; End; end else begin if (Transtypage[i] = 'Date') or (Transtypage[i] = 'Integer') then CommandeSQL := 'if not exists (Select Null from information_schema.columns where TABLE_NAME = ''' + Nom_Fichier + ''' and COLUMN_NAME = ''' + Table_en_cours.Fields[i].FieldName + ''') ALTER TABLE ' + Nom_Fichier + ' ADD ' + Table_en_cours.Fields[i].FieldName + ' ' + Transtypage[i] + ';' else CommandeSQL := 'if not exists (Select Null from information_schema.columns where TABLE_NAME = ''' + Nom_Fichier + ''' and COLUMN_NAME = ''' + Table_en_cours.Fields[i].FieldName + ''') ALTER TABLE ' + Nom_Fichier + ' ADD ' + Table_en_cours.Fields[i].FieldName + ' ' + Transtypage[i] + '(' + inttostr(Table_en_cours.Fields[i].DataSize) + ');'; if Table_en_cours.Fields[i].FieldName = 'DATE' then Begin Inc(NewChamp, 1); PosDate := i; End; if Table_en_cours.Fields[i].FieldName = 'HEURE' then Begin Inc(NewChamp, 1); PosHeure := i; End; end; End; Inc(i,1); Index := false; AdoQuery1.SQL.Add(CommandeSQL); AdoQuery1.ExecSQL; AdoQuery1.SQL.Clear; End; End; if NewChamp = 2 then Begin CommandeSQL := 'if not exists (Select Null from information_schema.columns where TABLE_NAME = ' + Nom_Fichier + 'and COLUMN_NAME = ' + Table_en_cours.Fields[i].FieldName + ') ALTER TABLE ' + Nom_Fichier + ' ADD DATE_HEURE Bigint NOT NULL;'; AdoQuery1.SQL.Add(CommandeSQL); AdoQuery1.ExecSQL; AdoQuery1.SQL.Clear; End;
La boucle tourne correctement pour les 200 premières colonnes. La table est également correctement créée. Au moment de créer la colonne 201, une violation d’accès apparaît. Au départ je faisais une seule requête SQL que j'appelais à la fin, mais la soupçonnant d'être trop lourd, j'ai décidé de faire une requête par colonne (Code ci-dessus) or ça n'a pas changé mon problème hormis la création des 200 premières colonnes pour rien du tout au part avant. ( vu que je n'arrivais pas sur le AdoExecSQL )
Je ne sais donc pas pourquoi j'ai ce problème, je ne sais pas non plus si Ado est limité en nombre d'exécution de requête. ( Ce qui m'étonnerait énormément.)
Ps: J'ai essayé de rajouter manuellement, et par requête, une nouvelle colonne directement depuis le SGBD, (Soupçonnant une limite imposée par SQL EXPRESS) la colonne se rajoute bien, donc à première vu un problème Delphi.