Temporary Index on the fly

Posted on 1998-01-29
Last Modified: 2010-05-18
How would I go about creating a temporary Index in a procedure that can be used in a TTable, but then can be discarded (somehow) either automatically or manually when the Table is closed or freed?

Question by:Whytboy
  • 3
  • 3

Accepted Solution

d4jaj1 earned 150 total points
ID: 1357849
Don't know of any way to create a temporay index, the best thing to do is create the index, then delete it when the table/form is closed.  To add the index, use;

if IndexName = Fieldname then ixCaseSensitive is reqd (the default)
if IndexName <> Fieldname then ixCaseInsensitive is reqd

Thus you need:

table.AddIndex('cusname', 'name', [ixCaseInsensitive]);
table.AddIndex('name', 'name', []);

To delete the Index, use; Table1.DeleteIndex('NewIndex');

Any questions, please add a comment.

Good Luck!

Author Comment

ID: 1357850
    Will this add the index to the table?  I ask, because basically, I have to keep everything READ-ONLY.  The table I am viewing and wanting to Re-Index is not of my own design and so I can't adjust the database in ANY way.  They have their indexes in the database, to which I can not add, modify, or delete!


Expert Comment

ID: 1357851
In that case, you have a problem.  There isn't a 'virual' index.  This will actually add an index to the table.

What do you need teh index for, sorting, filtering?
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


Author Comment

ID: 1357852
Its for sorting mainly... on a non-primary key or indexed value.  I did try using a SQL with the fieldname as the ORDER BY parameter... but, it didnt work either.

Expert Comment

ID: 1357853
Unfortunately, the Order By clause uses an Index to sort the DB.  Every other means of sorting a table, SetRange, IndexName, etc. require an index - afterall, that what they are there for.  Unless the owners of the DB allow you to add an index - at least only for a short time, you won't be able to sort this table.

There is a way you can provide your users a sorted view of this same data, although it will be slow.  It would require you to create a new table, just like the existing table, copy its data, then sort (apply the index) to the newly created table.  Add a button you your existing form that opens a new form and creates the table ---

   with table1 do
   tempdb := ttable.create(nil);
    with tempdb do begin
     DataBaseName := table1.databasename;
     tableName := 'temporary.db';
     TableType := table1.tabletype;
     with IndexDefs do
       Add('NewIndex', 'NewIndex', []);

Set the index and show the form here.


Author Comment

ID: 1357854
Yeah... Thats about what I came down to.. now the problem is "sync"ing the tables..but, thats a different question

Expert Comment

ID: 1357855
You could create a new field in the table only for sorting and then when the user want to sort you could copy all fieldvalues to that "SortField"... Slow again ofcourse

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 143
find a node in VST 2 69
How to define IfThen functions in one common unit? 4 54
How to make Sign in, using Clientdataset? 1 19
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question