devxlogo

TQuery command to use to execute a SQL statement

TQuery command to use to execute a SQL statement

Question:
I’m trying to delete multiple records using Tquerysuch as

delete from products p   where p.shopno in     (select shopno         from shops)
When I run the program, it executes the statements and deletes matching records but gives me an error message such as ‘error creating cursor handle’.What is the reason and what can I do?

Answer:
The only reason I can think of for this is that instead of issuing theExecSQL command to execute the DELETE, you’re issuing the Open command. Hereare a couple of rules you should follow when using SQL in Delphi:

  1. Use the Open command for a TQuery or one of its descendants when you want to return a result set from the query that will be in theform of either a cursor handle or a batch moved to a persistent data store (TTable).This includes calling stored procedures called within the context of a SQLstatement (i.e., SELECT * FROM MyStoredProc).
  2. Use the ExecSQL command for a TQuery or one of its descendants when the SQL operation does not return a result set. This applies to INSERT,UPDATE and DELETE queries that are table maintenance queries.
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist