Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a new table with variable field names

Status
Not open for further replies.

HAINC

IS-IT--Management
Mar 10, 2011
8
US
I am trying to create a temporary table in code so can easly append records to another table. My new table it.dbf has data1 and data2 as the field names I would like to have F1 and F2 as the field names. My code looks something like this:

Store F1 to data1
store F2 to data2
CREATE TABLE c:\IT (Data1 C(10), Data2 C(10))
 
Do you want to ALWAYS have your field names be 'F1' & 'F2'?
Code:
CREATE TABLE c:\temp\temp2 FREE (F1 C(10), F2 C(10))

Or do you want to dynamically create a data table which has field names based on the contents of 2 variables?
Code:
cField1 = "FIELD111"
cField2 = "FIELD222"
CREATE TABLE c:\temp\temp2 FREE (&cField1 C(10), &cField2 C(10))

Good Luck,
JRB-Bldr


 
That should be this:

Code:
Store "F1" to data1
store "F2" to data2
CREATE TABLE c:\IT (&Data1 C(10), &Data2 C(10))

It looks like all I did was add punctuation, but every character is necessary and has meaning.
 
It's not clear what your problem is. If you want to create a table whose field names are F1 and F2, then just do this:

Code:
CREATE TABLE c:\IT (F1 C(10), F2 C(10))

If that's not what you want, perhaps you could clarify the problem.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
trying to create a temporary table"

Suggestion, instead of creating a Temp Table, consider just creating a (SQL) Cursor. 99.9% the cursor (IMO) is a better choice.

Code:
CREATE CURSOR IT (Data1 C(10), Data2 C(10))

No need to have code to look for and delete the temp table on the harddrive before creating a new one, no need to delete it after it is no longer needed, no worries about disk security, no issues with another user using the table when you need it, no issues of wrong datasession, etc, etc, etc Again IMO the Cursor is a much better choice.

Of course if you tell me that the DBF is needed later, then OK create an actual table, but in that case the table is not temporary.


Lion Crest Software Services
Anthony L. Testi
President
 
You can even put together the whole CREATE statement and then execute it by macro substitution, eg:

Code:
data1="f1"
data2="f2"
cursoralias = "it"
lcCreateCursor = Textmerge("CREATE CURSOR <<cursoralias>> (<<data1>> C(10), <<data2>> C(10))")
&lcCreateCursor

This opens even more possibilities, eg variable field length defined by other variables, etc. You can go up to meta data driven database generation.

And by the way: Despite of SQL Server, where cursors are mostly avaoided, as they are a per user server resource, in VFP cursors are of course local and mainly stay in RAM, as long as cursor don't expands to high memory consumtion, then it's written out to local disc TEMP dir. With today RAM equipment this often enough means 1GB or even more Cursors in RAM. It's fast, faster than SSD, it's RAM. Use it where you don't need data persitent, eg it's meant for temp data and results. Also not limited to short time intervals. Cursors are the working horse of foxpro.

Bye, Olaf.
 
As you say you want to "easily append records to another table". You can simply copy the structure of the destination table to a tmp table or cursor via:

Code:
USE desttable
COPY STRUCTURE To It.dbf

Or, to stay with cursors:
Code:
Use desttable
Afields(laFields)
CREATE CURSOR It FROM ARRAY laFields

or perhaps simpler in one sql:
Code:
Select * From desttable Into Cursor IT READWRITE Where .F.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top