سؤال

I´m having trouble to fetch IDENTITY columns with ADO in Delphi XE2.

Question The ADO Fields property doesen´t return/contain any IDENTITY columns. Is there a property or something in the connection that will enable to fetch IDENTITY columns or is this a known bug ?

Scenario I have a stored procedure (in SYBASE ASE), that fetch 1 row with 4 columns. One of the column is a IDENTITY column.

In this example: ADOQuery.Fields.Count will return 3 and not 4. The IDENTITY column is missing.

Ex:

procedure TForm1.FormCreate(Sender: TObject);
var
test: string;
Password: String;
UserName: string;
ServerName : string;
NoRec: integer;
MyValue: integer;

begin

Password:='xxxx';
UserName:='yyyy';
ServerName := 'zzzzz';

ADOConnection := TADOConnection.Create(Application);

with ADOConnection do
begin
  CommandTimeout := 0;
  IsolationLevel := ilSerializable;
  Attributes := [xaAbortRetaining];
  KeepConnection := True;
  LoginPrompt := False;

   ConnectionString := 'Provider=ASEOLEDB.1;Password=' + Password +
        ';Persist Security Info=True' + ';User ID=' +
        UserName + ';Data Source=' + ServerName; 
end;

if not Assigned(ADOQuery) then
begin
  ADOQuery := TADOQuery.Create(Application);
  with ADOQuery do
  begin
    CommandTimeout := 0;
    DisableControls;

    CacheSize := 500;
    Connection := ADOConnection;
    CursorType := ctOpenForwardOnly;
  end;
end;

ADOConnection.Open();
ADOQuery.SQL.Text:= 'sp_echo';
ADOQuery.Open;

NoRec:=ADOQuery.Fields.Count;  //This will return: 3, the IDENTITY col is missing

//Trying to fetch the column this way, will fail (the field doesen´t exist):
//MyValue:=ADOQUery.FieldByName('col_1').AsInteger
end;

Here is script, table, stored procedure for this example

create table tbl_echo
(
  col_1  numeric(10,0) identity,
  col_2  varchar(255),  
  col_3  int,
  col_4  int
)

insert into tbl_echo (col_2, col_3, col_4) select 'testing', 100, 200

create proc sp_echo
as
select top 1 * from tbl_echo
هل كانت مفيدة؟

المحلول 2

Okey, I´ve tried to use some other providers with the same result.

One simple solution for this problem is just to cast the IDENTITY column to an integer in my procedure/query. By doing that it works like expected.

create proc sp_echo
as
select top 1  cast(col_1 as int) as col_1, col_2, col_3, col_4 from tbl_echo

نصائح أخرى

You have two different ways to get what you want:

  1. You declare an output parameter in your SP and, after inserting, assign @@IDENTITY to this parameter. Execute such a SP by using a TADOStoredProc component, not a TADOQuery component. After the SP finnishes, when the control comes back to your Delphi code you just read the value of such a parameter.

  2. You simply select @@IDENTITY, what will give you a result set with a single row with a single column containing the generated value. In this case you can use a TADOQuery, but in your Delphi code you use Open (not ExecSQL) to retrieve such a resultset and just read the value from the only field is has.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top