Subject Using a Generator when creating a new record
Author Stevio
I'm having a problem using a Generator when creating a new record in my
Firebird table, perhaps you could help me. I am using the TIBDataSet
component from InterBase Express 5.02.

The GeneratorField of the component is set to:
GEN_NEW_AUTHOR_ID -> AUTHOR_ID By 1
The ApplyEvent option is set to On New Record.

The Generator has a current value of 16293 and there are 16292 records in
the table.

The code I use in Delphi is as follows:
DataSet.Open;
DataSet.Append;
DataSet.FieldByName('NAME').Value := edtAuthorName.Text;
DataSet.Post;

The InsertSQL of the TIBDataSet is as follows:
insert into AUTHOR
(AUTHOR_ID, NAME)
values
(:AUTHOR_ID, :NAME)

The error I receive is
Field 'AUTHOR_ID' must have a value.

I used the following code to create the generator:
CREATE GENERATOR GEN_NEW_AUTHOR_ID;
SELECT GEN_ID(GEN_NEW_AUTHOR_ID, (SELECT MAX (AUTHOR_ID) FROM AUTHOR)) FROM
RDB$DATABASE;

SET TERM ^ ;
CREATE TRIGGER SET_NEW_AUTHOR_ID FOR AUTHOR
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.AUTHOR_ID IS NULL) THEN NEW.AUTHOR_ID = GEN_ID(GEN_NEW_AUTHOR_ID,
1);
END ^

CREATE PROCEDURE PROC_NEW_AUTHOR_ID
RETURNS (THE_NEW_AUTHOR_ID INTEGER)
AS
BEGIN
THE_NEW_AUTHOR_ID = GEN_ID(GEN_NEW_AUTHOR_ID, 1);
END ^
SET TERM ; ^

Any ideas what the problem is?

Thanks much,
Stephen