TIBQuery, TIBTable and TIBDataset exist to support data aware controls e.g. TDBGrid, TDBEdit, etc. They provide a buffered dataset that is the result of a "select" query. TIBDataset also allows you to provide supporting Insert, Update, Delete and Refresh queries that allow any changes made to the dataset to be written back to the database.You can also add a TIBUpdateSQL component to a form with a TIBQuery, and link it to the TIBQuery. The TIBUpdateSQL also allows you to add supporting Insert, Update, Delete and Refresh queries to the TIBQuery's dataset - again to any changes made to the dataset to be written back to the database. TIBQuery + TIBUpdateSQL is equivalent to a TIBDataset.
TIBSQL is a component that supports direct execution of any database query (Select, Insert, Update, Delete, Execute Procedure). You use it when you need programmatic access to the database but do not need to support any data aware controls.
For example, if you only need to insert a row into a database (using the query from your other question), then TIBSQL can do this inline:
with TIBSQL.Create(nil) do
try
Database := MyDatabase; {also sets the TIBSQL transaction to the default database transaction}
SQL.Text := 'Insert into TBKONTRAKTE(NAME_,COMM,JAHR_AUS_NAME,MONATSKENNZAHL)' + // ID_KONTRAKT,
' values (:NAME_,:COMM,:JAHR_AUS_NAME,:MONATSKENNZAHL) returning ID_KONTRACT';
ParamByName('NAME_').AsString := Trim(name_);
ParamByName('COMM').AsString := Trim(comm);
ParamByName('JAHR_AUS_NAME').asSmallInt := Jahr_aus_Namen;
ExecQuery;
MyContractID := FieldByName('ID_KONTRAKT').AsInteger;
finally
Free
end;
A select query can be similarly executed. You use TIBSQL.FetchNext to scroll between rows (unidirectional only) and TIBSQL.Close when you are done.
If you need to display the results of a TIBSQL.ExecQuery in an already open TIBQuery, etc. then you have to call TIBQuery.Refresh in order to force it to re-read the dataset from the database. In this case, you should also use the same transaction for both the TIBSQL and the TIBQuery.
If you really want advanced usage then you can also make direct use of the Firebird Pascal API to achieve the above in one line. e..g
const
sql = 'Insert into TBKONTRAKTE(NAME_,COMM,JAHR_AUS_NAME,MONATSKENNZAHL)' + // ID_KONTRAKT,
' values (:NAME_,:COMM,:JAHR_AUS_NAME,:MONATSKENNZAHL) returning ID_KONTRACT';
MyContractID := MyDatabase.Attachment.ExecuteSQL(MyTransaction.TransactionIntf,sql,[Trim(name_),Trim(comm),Jahr_aus_Namen])[0].AsInteger;
If you dive into the underlying code for TIBSQL, you'fl find that it is no more than a wrapper for the above.
I hope that this is useful.