3

I'm struggling with correct procedure for executing SQL query.

Basically, I have text field where user can enter SQL code and program will execute it. Unfortunately, I don't know if the dataset will be returned, so I cant tell which function to use: ADOQuery.Open or ADOQuery.ExecSQL

But I need to make some calculations if there is results returned.

Is there any way of predicting if query will return some result or if it is UPDATE only ... how to handle this situation ?

Arthur
  • 43
  • 1
  • 4

3 Answers3

4

Forget about TADOQuery, TADOTable, TADOStoredProc. They are components designed to ease porting from applications using the BDE that used their counterparts. In your situation you can use TADOCommand, its Execute() method will return a recorset when needed, and you can access it via a TADODataset assigning Execute() return value to its RecordSet property.

0

Without completely parsing and analysing the query text, you can not tell for sure.

But you can (and in your case should) use ADOQuery.Open everytime, as in the case of a not returning command, an empty resultset will be returned - which you can easily detect via TADOQuery.Bof and TADOQuery.Eof or a more low-level property TADOQuery.Recordset.

Viktor Svub
  • 1,451
  • 10
  • 15
  • If you are using `TADOQuery.Open` for a query that is not supposed to return data, an exception is raised. Same goes for `TADOStoredProc.Open`. – Andriy M Apr 18 '11 at 19:39
0

The best way is to analyse the query before execute it. You will found an exemple of 'how to' with this open source software

philnext
  • 3,242
  • 5
  • 39
  • 62