Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Automatically generating a 'create table' command
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00953298
Message ID:
00953361
Views:
14
>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
   *** what happened display error
endtry
return .t.
Steven D. Supinski
The American Contractor
Santa Cruz, CA 95062
Phone: (800) 333-8435 ext 4017
Email: ssupinski@theamericancontractor.com
Previous
Reply
Map
View

Click here to load this message in the networking platform