Temporary Index on the fly

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?

Who is Participating?
d4jaj1Connect With a Mentor Commented:
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!
WhytboyAuthor Commented:
    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!

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?
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

WhytboyAuthor Commented:
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.
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.

WhytboyAuthor Commented:
Yeah... Thats about what I came down to.. now the problem is "sync"ing the tables..but, thats a different question
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.