4

I have a TADOQuery component which is connected to an SQL Server database using TADOConnection. Now, when I add below SQL to the component, and call ExecSql its raising proper exception, because there is a typo in the 'Create' word.

 exec ('creat1e table myTable 
     (
          col1     TPT_Dt       not null ,
          col2     TPT_FLOAT       not null
     )');

But the same statement, if I add it to another statement, then the TADOQuery component ignores this exception. E.g. :

 select * from SomeOtherTable where id = 10

 exec ('creat1e table myTable 
     (
          col1     DtDataType       not null ,
          col2     FLOATDataType       not null
     )');

Could you please tell me why Delphi ignores the exception in the second case.

my issue is not with the select statement. even if i add an if condition then also same issue ;

for example

if not exists (select * from dbo.sysobjects where id = object_id('TABLE1278') and type='U')
begin
 exec ('crea11te table table1278 
     (
          col1     TPT_Dt     
     )');
end

delphi simply ignores the typo.

Thanks in advance.

Basil

iambasiljoy
  • 319
  • 3
  • 11
  • I guess because the first statement is successful and ADO returns a dataset along with the related error message(s) – iMan Biglari Jan 15 '13 at 14:15
  • do you execute `if not exists...` **without** a select first? if yes then I cannot reproduce. if you do you a select before, then I think the issue is with SQL `exec`. if I omit the exec and use `crea11te` statement then an error is raised. Ive tested it with `ADOCinnection.Execute` with `eoExecuteNoRecords` (default) and this should not even try and rerun a recordset like TADOQuery. interesting question anyway. – kobik Jan 15 '13 at 15:19
  • [This question](http://stackoverflow.com/questions/10519912/ado-command-running-multiple-sql-statements-cant-get-error-message-back-use-t) may be related. the [resolution](http://support.microsoft.com/kb/254304) provided by Microsoft (`SET NOCOUNT ON`) did not helped for me. – kobik Jan 15 '13 at 17:01

1 Answers1

2

All recordsets generated from a query are returned. The first one will be show by default, with corresponding error.
You can access all recordsets via ADODataSet1.NextRecordset(a).

e.g. you have following query

select * from TableWhichExists

Select * from TableWichNotExists

You will fetch result of TableWhichExists.

With

procedure TForm1.Button1Click(Sender: TObject);
var
 rs:_RecordSet;
 a:Integer;
begin
  rs := ADODataSet1.NextRecordset(a)
end;

you can access the next recordset und will receive the corresponding error.

NextRecordset may be usefull e.g. to receive several recordset via procedure, calculatin once all needed recordset.

To use e.g. three received results you could work like this:

procedure TForm1.Button1Click(Sender: TObject);
var
 a:Integer;
begin
  Adodataset2.Recordset := ADODataSet1.NextRecordset(a) ;
  Adodataset3.Recordset := ADODataSet1.NextRecordset(a)
end;

Adodataset1 would display the first result.

bummi
  • 27,123
  • 14
  • 62
  • 101