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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COPY INDEXES 4

Status
Not open for further replies.

ameedoo3000

IS-IT--Management
Sep 20, 2016
233
EG
Hi all
how can i copy the INDEXES from table to another table

 
Assuming that you have a compound index (CDX file), you could do something like this:

Code:
SELECT SourceTable
lnCount = ATAGINFO(laTags)
SELECT TargetTable
FOR lnI = 1 TO lnCount
  lcExpr = laTags(lnI, 3)
  lcName = laTags(lnI, 1)
  INDEX ON &lcExpr TAG &lcName
ENDFOR

I haven't tested this, but I think it should mor-or-less work. You will need to adjust it if you also want to specify a collating sequence and an index order (ascending vs descending).

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
can i do this through the COPY INDEXES command
I think it would be easier but I do not know how to write this command
 
You can copy index with table when you USE someold.dbf and COPY TO somenew.DBF WITH CDX

That's all to it, all tags are copied, too.

Bye, Olaf.

Olaf Doschke Software Engineering
 
COPY INDEXES is a command to integrate IDX indexes into a CDX file. COPY INDEXES doesn't exist to copy an index from one to another table. Just read the help.

COPY TO WITH CDX also works with a FOR clause not copying all data, the index tags then also are just built for the copied data. Copying indexes separately after you already copied data or did SQL-Select INTO TABLE is not working that easy, you will need to know the index expression and recreate the index by it, and that does not work for all index types, PRIMARY indexes cannot be created by INDEX ON, for example.

So the best way to have a table with same indexes as another is via COPY TO.

You could also go the much more general route of recreating tables with the help of the code GENDBC creates from a DBC (also available as GENDBCX in some VFPX github), which means all the CREATE TABLE, INDEX ON and other commands necessary to create some DBC, with its DBFs CDXes. And then pull out whatever partial things you need.

Bye, Olaf.

Olaf Doschke Software Engineering
 
can i do this through the COPY INDEXES command

No. If you read the Help for COPY INDEXES, you will see that it is used to create a compound index file (a CDX file) from individual IDX files, which is not what you want.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike :i want that but i need one example because help not include example for that .
 
amedoo3000 said:
Mike :i want that

No, you don't. You asked:
amedoo3000 said:
how can i copy the INDEXES from table to [highlight #FCE94F]another[/highlight] table
And this is not what COPY INDEXES offers, it turns IDX into CDX for the same table.

Notice copying an index including its data can only work out and result in an index pointing a seek inside this index to the correct records, if not only the index but also the data matches. For the fourth or fifth time from three persons, I point out the command you could use while you create the table data copy, too, is SELECT source and then COPY TO target.DBF WITH CDX.

It's clear that your situation is having two tables. You can't copy over the index from one to another table, it's not sufficient that the structure is the same.

What you can do when you have other data, just partial data or even an empty second DBF is what Mike showed you in his first answer, you can copy over the index expressions and recreate the same index tag names with the same expression to let the same SEEK/INDEXSEEK or rushmore optimizations work with the second table, too.

There is neither a source nor a target alias as option of the COPY INDEXES command. You miss to understand the syntax description. You specify tag names associated with IDX file names and a CDX file name as target. The best you could do is copy from table1tag1.idx, table1tag2.idx to table2.cdx, but the CDX you create that way won't work! It's not designed for table2, even if table2 is a structural copy. The index data points to records in table1.dbf and all the record numbers would be wrong for table2, SEEKing in that cdx would guide you to totally wrong records.

Again, in your situation you have a simpler solution if you go back to the step of creating the second table with data AND cdx at the same time with COPY TO WITH CDX or you have to iterate all indexes of the first table and recreate them with INDEX ON, as far as that's possible (not with primary indexes). You better change the code creating the second table to do that with COPY TO WITH CDX as that's the step where you can copy the indexes, all of them, even primary.

Bye, Olaf.

Olaf Doschke Software Engineering
 
I wish people wrote more in depth about what they really want, and that's for several reasons. One is so that we don' misunderstand the question. Another is that we may have a better solution to what the OP actually want to accomplish.
 
Well all of the above is good and excellent but what if I wanted to move information from table A to table B with the same index of table A by simple steps
I was thinking of copying Table A to Table B and then clearing all the data of Table B ..... Now how can I transfer all the data of Table A to Table B in this case?
 
And to get an empty tableB with indexes:
Code:
Use tableA
Copy To tableB with CDX FOR .F.
This just uses very basic knowledge about how FOR clauses work.

Bye, Olaf.

Olaf Doschke Software Engineering
 
I already answered that question. Do you read the answers at all, before you ask?
 
thank for all
now every thing is Ok

Greetings

Ahmed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top