>Has anyone written a 'Create Table command' generation program that will create that command by evaluating a real table?
>
>If so, do you mind sharing? We have several large tables (many fields) that we want to start creating programmatically, and I don't want to manually build the entire 'CREATE TABLE' command.
>
>Thanks, Michael
You can do most of it with 2 commands AFIELDS() and CREATE TABLE from ARRAY
Something like =AFIELDS(aFirstTableData,"C:\TABLES\MYTABLE.DBF") followed by CREATE TABLE c:\tables\MyNewTable.DBF FROM ARRAY aFirstTableData
The thing you don't get are the indexes from that. You could do a simple file copy of the CDX file to a new file (with the same name as you gave the new table) and then open the new table and set the index to the newly created index and reindex it. Voila! Your done.
So make a function that takes as a parameter the original table filespec and the desired new tables filespec. Something like the following but with a lot of cleanup :)
FUNCTION MakeNewTable
PARAMETERS SrcTable,DestTable
IF !FILE(m.SrcTable)
=MESSAGEBOX("Can't locate the source table. Aborting!",16,"Alert")
RETURN .F.
ENDIF
TRY
use (m.SrcTable) ALIAS Source
=AFIELDS(aSrcData,"Source")
USE IN SOURCE
CREATE TABLE (m.DestTable) FROM ARRAY aSrcData
USE
IF FILE(STRTRAN(m.SrcTable,".DBF",".CDX"))
COPY FILE (STRTRAN(m.SrcTable,".DBF",".CDX")) TO (STRTRAN(m.DestTable,".DBF",".CDX"))
ENDIF
USE (m.DestTable) alias DestTable in 0 exclusively
select DestTable
set index to (STRTRAN(m.DestTable,".DBF",".CDX"))
reindex
use
catch to oError
endtry
return .t.