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
TIBCMetaData and primary key
-
AndreyZ
Hello,
The easiest way to obtain primary key fields is to use the TIBCQuery.QueryKeyFields method. Here is an example:With the TIBCMetaData component, you can obtain the same result using the following code:
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);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;