Subject Firebird Triggers not working
Author johnkebert
Hi all,

D6Pro/Win2000
Okay, I am trying to go to Firebird 1.5 using IBO components from
MySQL using ZEOS components.

In MySQL I have autoinc fields as primary keys, so to try to
duplicate that, I have set up Generators and Triggers in Firebird -
here is a typical setup:

procedure TdmData.CreateWineTable(Sender: TObject);
begin
With ibqWine do //this is a TIB_Query
Try
Active := False;
Sql.Clear;
Sql.Add('CREATE TABLE WINE (WINEID integer NOT NULL,');
Sql.Add(' WINENUM varchar(10),');
Sql.Add(' WINETYPE varchar(25),');
Sql.Add(' FRUITSOURCE varchar(25),');
Sql.Add(' STARTDATE date,');
Sql.Add(' QUANTITY varchar(15),');
<snipped a bunch of fields>
Sql.Add(' BOTTLETA varchar(5),');
Sql.Add(' BOTTLES varchar(7),');
Sql.Add(' ARCHIVED smallint DEFAULT ''-1'',');
Sql.Add(' PRIMARY KEY(WINEID));');
ExecSQL;
SQL.Clear;
Sql.Add('CREATE INDEX WINESORT ON WINE (STARTDATE);');
ExecSql;
ibtransWine.Commit;
SQL.Clear;
Sql.Add('CREATE GENERATOR WINEID_GEN;');
ExecSql;
ibtransWine.Commit;
SQL.Clear;
Sql.Add('CREATE TRIGGER CREATE_WINEID FOR WINE');
Sql.Add(' BEFORE INSERT');
Sql.Add(' POSITION 0');
Sql.Add(' AS BEGIN');
Sql.Add(' IF(NEW.WINEID IS NULL) then NEW.WINEID=GEN_ID(WINEID_GEN,
1);');
Sql.Add(' END');
ExecSql;
ibtransWine.Commit;
Sql.Clear;
Sql.Add('SELECT * FROM WINE');
Sql.Add(' WHERE ARCHIVED = -1');
Sql.Add(' ORDER BY STARTDATE');
Active := True;
Except { If an error occurs, the Table Not created tell me }
on E: Exception do
begin
Showmessage(E.Message);
MessageDlg('What the ?? An Error. Wine Table NOT Created.',
mtError, [mbOK], 0);
end;
End;
end;

I have viewed the triggers and generators via isql.exe and Database
workbench, which say they are active. I have tried with and without
the if clause in the trigger. However upon entering data via a grid
or using a procedure with Append to the ibqWine query on post, I get
the error "WINEID is a required field."

So am I missing something here? It seems the generator/trigger
doesn't work.

Any tips/comments greatly appreciated,

John