Here is a very simplistic tutorial on how to make stored procedures in SQL SERVER. You can apply the same concept to most other RDBMS you want to use.
https://www.mssqltips.com/sqlservertutorial/168/different-options-for-creati...
If you do this you can stop writing SQL CODE in VFP and pass it to the backend where that db engine has to make a plan to execute the query every time. If you have that SQL Code in a stored procedure the db engine can look it its stats to determine the best plan to execute the code.
I have some long stored procedures that are in the 3000 lines long arena that are called for every Bill of Lading report we print. Granted it combines Sales data, testing data, item specifications data as well as customer and our employee system data. This generates a CofA or Certificate of Analysis report for every lot number on the truck.
Sprocs are great because they make it much harder to hack into your backend data. Nothing new here, right?