Author Topic: Sorting tsqlquery with indexes - new question  (Read 197 times)


  • Full Member
  • ***
  • Posts: 138
Sorting tsqlquery with indexes - new question
« on: January 19, 2020, 07:05:07 pm »
I have used the code by Ericktux from this post:,20142.msg207605.html
and it works wonderful!
There are some things that I'm not sure how to deal with. My case is when a dataset can be initially sorted by using SQL SELECT with ORDER BY statement; and ordering key may be set by application user.

I would like to show the appropriate arrow in the appropriate column title of the DBGrid after opening SQLQuery. I can do this manually, knowing the "ORDER BY" key. But maybe it is easy to discover this automatically? Or maybe it is better not to use "ORDER BY" at all and set SQLQuery indexes instead?

In my application SQLQuery can be opened again and again with modified SQL texts. If previously DBGrid was sorted with mentioned procedure, then the SQLQuery index is kept for next SQL request, and it overrides the "ORDER BY". What is the correct way to drop the previously set index of SQLQuery?

I didn't find much information about using TSQLQuery.IndexDefs...


  • Hero Member
  • *****
  • Posts: 970
Re: Sorting tsqlquery with indexes - new question
« Reply #1 on: January 20, 2020, 09:10:43 am »
You can use indexfieldnames to sort @runtime. Normally it's better to use ORDER BY, but it actally calles a new request to the server. If 200 people are doing this regulary, you get an heavy load on the server.
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10