Page 1 of 1

TIBCMetaData and primary key

Posted: Fri 10 Feb 2012 11:07
by Fabrice
Hello,

I'am looking for a sample to list all field name which are primary key of a table. TIBCMetaData seems to be one of the components without example.
For exemple if :

Create table mysample
(
ID integer not null,
ID2 intger not null,
Name varchar(30),
primary key (ID, ID2)
)

I would like : ID and ID2 has result.

I have successfully done it to extract tablename, fieldname but I don't know how to extract "primary key" fields.

Best regards,
Fabrice

Posted: Fri 10 Feb 2012 13:37
by AndreyZ
Hello,

The easiest way to obtain primary key fields is to use the TIBCQuery.QueryKeyFields method. Here is an example:

Code: Select all

IBCQuery.QueryKeyFields('your_table_name', Memo.Lines);
With the TIBCMetaData component, you can obtain the same result using the following code:

Code: Select all

procedure TMainForm.BitBtnClick(Sender: TObject);
var
  ts: string;
begin
  IBCMetaData.MetaDataKind := 'Constraints';
  IBCMetaData.Restrictions.Values['TABLE_NAME'] := 'your_table_name';
  IBCMetaData.Restrictions.Values['CONSTRAINT_TYPE'] := 'PRIMARY KEY';
  IBCMetaData.Open;
  if not IBCMetaData.Eof then begin
    ts := IBCMetaData.FieldByName('INDEX_NAME').AsString;
    IBCMetaData.Close;
    IBCMetaData.MetaDataKind := 'IndexColumns';
    IBCMetaData.Restrictions.Values['INDEX_NAME'] := ts;
    IBCMetaData.Open;
    while not IBCMetaData.Eof do begin
      Memo.Lines.Add(IBCMetaData.FieldByName('COLUMN_NAME').AsString);
      IBCMetaData.Next;
    end;
  end;
end;

Posted: Fri 10 Feb 2012 14:46
by Fabrice
Thank you for your quick and efficient answer, it's work like a charm.

Best regards,
Fabrice

Posted: Fri 10 Feb 2012 15:07
by AndreyZ
I'm glad I could help. If any other questions come up, please contact us.