Subject Re: [IBO] StoredProc Error
Author Helen Borrie (TeamIBO)
At 02:41 PM 15-03-02 -0800, you wrote:
>Hi
>
>I have just started using IBObjects and have a problem running a stored
>proc.
>
>I have a proc defined in the database as so
>
>create procedure nextidaddress
>returns (NEXTID integer)
>as
>begin
> NextID = GEN_ID(AddressGen, 1);
> suspend;
> exit;
>end
>
>which runs fine. Im trying to call it through code by issuing -
>
> FStoredProc.IB_Connection := FDataTarget;
> FStoredProc.StoredProcName := AProcName;
> FStoredProc.ParamNames.Clear;
> FStoredProc.Prepare;
> FStoredProc.ExecProc;
> Result := FStoredProc.FieldByName('NEXTID').AsInteger;
> FStoredProc.Unprepare;
> FStoredProc.IB_Transaction.Commit;
>
>But when I do I keep getting 'Invalid Transaction handle (expecting explicit
>transaction start)' at the Prepare statement which I thought was because I
>havent issued an FStoreProc.StartTransaction, even if I add this line of
>code

StartTransaction is a method of a transaction, not of a stored
procedure. This code could be corrected like this:

with FStoredProc do
begin
IB_Connection := FDataTarget;
if not IB_Connection.Connected then
IB_Connection.Connect;
StoredProcName := AProcName;
if not IB_Transaction.TransactionIsActive then
IB_Transaction.StartTransaction;
Params.Clear;
Prepare;
Params[0].Value := something;
Params[1].Value := somethingelse;
Execute;
Result := FieldByName('NEXTID').AsInteger;
if Prepared then Unprepare;
IB_Transaction.Commit;
end;

However, there is no need to have your own procedure to do any of
this. Either set the GeneratorLinks in the dataset that wants the
generated value, or call the Gen_ID() method of either the dataset or the
IB_Connection.

Either (GeneratorLinks property)
MyColumn=AddressGen

or (if you want to do something with the value before the Insert is posted):

MyDataset.FieldByName('Somefile').AsInteger := MyDataset.GEN_ID(AddressGen, 1);

regards,
Helen Borrie (TeamIBO Support)

** Please don't email your support questions privately **
Ask on the list and everyone benefits
Don't forget the IB Objects online FAQ - link from any page at
www.ibobjects.com