L'objectif étant par exemple de pouvoir archiver certaine tables dans une base archive (ces tables étant supposée à structure dynamique donc non connue à l'avance...)

Pour ma problématique j'ai fait un bout de code qui marche mais long et pas très satisfaisant avec une requête, d'ou ma question...:

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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 
...//Création de la nouvelle table avec les champs nécessaires
 
StarDataModule->LocalQuery->SQL->Clear(); // efface existant
StarDataModule->LocalQuery->SQL->Add("CREATE TABLE " + TableName + " (");
 
Memo = "CREATE TABLE " + TableName + " (";
 
StarDataModule->OutilStoredProc->GetFieldNames(ListeChamps); //Remplit avec liste de Champs
for(int i = 0; i < ListeChamps->Count ; i++)
	{
	DefChamps = StarDataModule->OutilStoredProc->FieldDefList->FieldDefs[i]->DataType;
	LongChamps = StarDataModule->OutilStoredProc->FieldDefList->FieldDefs[i]->Size;
	CompoChamps = ListeChamps->Strings[i];
	switch (DefChamps) {
		case ftString:
		case ftFixedChar:
		case ftWideString:
			CompoChamps += " CHAR("+ IntToStr(LongChamps) + ")";
			break;
 
		case ftSmallint:
		case ftWord:
			CompoChamps += " SMALLINT";
			break;
 
		case ftBoolean:
			CompoChamps += " LOGICAL";
			break;
 
		case ftCurrency:
			CompoChamps += " CURRENCY";
			break;
 
 
		case ftInteger:
			CompoChamps += " INTEGER";
			break;
 
		case ftFloat:
			CompoChamps += " FLOAT";
			break;
 
		case ftBCD:
		case ftFMTBcd:
			//CompoChamps += " NUMERIC";  // Normalement c'est Numéric mais dans ce cas n'affiche pas les décimales
			CompoChamps += " CURRENCY";
			break;
 
		case ftDate:
			CompoChamps += " DATE";
			break;
 
		case ftTime:
			CompoChamps += " TIME";
			break;
 
		case ftDateTime:
		case ftTimeStamp:
			CompoChamps += " DATETIME";
			break;
 
		case ftBytes:
			CompoChamps += " BYTE";
			break;
 
		case ftMemo:
		case ftFmtMemo:
			CompoChamps += " MEMO";
			break;
 
		case ftGraphic:
			CompoChamps += " IMAGE";
			break;
 
		case ftTypedBinary:
			CompoChamps += " BINARY";
			break;
 
		case ftLargeint:
			CompoChamps += " DOUBLE";
			break;
 
		default:
			CompoChamps += " DOUBLE";
			break;
 
		}
 
	if(i != ListeChamps->Count -1)
		{
		CompoChamps += ",";                    // entre les champs
		StarDataModule->LocalQuery->SQL->Add(CompoChamps);
		}
	else
		{
		CompoChamps += ")";                  // en fin de création
		StarDataModule->LocalQuery->SQL->Add(CompoChamps);
		}
	Memo +=  CompoChamps;      // nes sert qu'en débug pour voir et copier dans requête Access et tester en cas de PB
	}
 
StarDataModule->LocalQuery->ExecSQL(); // Création de la table
 
// Recopie les enregistrtements dans la table Tampon afin de lancer ensuite la seconde procédure
 
StarDataModule->LocalQuery->SQL->Clear(); // efface existant
StarDataModule->LocalQuery->SQL->Add("SELECT * FROM " + TableName);  //Sélectionne la table destination
StarDataModule->LocalQuery->Active = true; // Active le dataset
 
StarDataModule->OutilStoredProc->First();
											// Recopie tous les records
for(int i = 0; i < StarDataModule->OutilStoredProc->RecordCount ; i++)
	{
	StarDataModule->LocalQuery->Append();
	for(int u = 0; u < StarDataModule->OutilStoredProc->FieldCount; u++)  // tous les champs
		{
		StarDataModule->LocalQuery->Fields->Fields[u]->Value = StarDataModule->OutilStoredProc->Fields->Fields[u]->Value;
		}
	StarDataModule->LocalQuery->Post();
	StarDataModule->OutilStoredProc->Next();
	}
 
StarDataModule->LocalQuery->Requery();     // permet de bien forcer la mise à jour des enregistrements!!! sinon pas à jour pour la suite
StarDataModule->LocalQuery->Active = false;
delete ListeChamps;
 
}