delphi – Abort running a long-running SQL


I have a very slow SQL (more than 10 minutes) to return the data.
However, it runs on a Thread and therefore the MainThread continues to run normally. While SQL is running, the user can shut down the system, and therein lies the problem.
Because the system even closes, but the process continues to run in the task manager until SQL returns the requested data.

Is it possible to abort the SQL execution immediately?


  • Delphi XE 7
  • FireDAC
  • Firebird
  • It's no use changing SQL to get faster, it's really slow and there's no way around it.


Here's a suggestion: Create a table structure in your database or create a folder to receive text files with XML/Json content.

After creating the structure above, instead of making a thread and performing the SQL query in this thread, do the following:

Create a WebService or a DLL to perform the method call with SQL, in this method, generate an ID and return it to your caller. Still inside your WS/COM, execute the query and after execution, create an XML/JSON with the data and save it in a database or in the folder informing the previously informed ID. Example: 89.txt(If saving in folder).

Now it is possible to create a thread that consults the folder/DB from time to time to check if that ID has already been generated and thus present the result in your application.

If your application has been closed, there will be no pending processing in case management.

I hope the idea is clear.

Scroll to Top