Java trigger examples

Posted by shuttleworth on 10-Jul-2012 16:41

I've searched through the documentation only to find very brief examples of java triggers which makes coding your own very hard with limited Java exposure.

Can anyone point me to documentation that I might have missed or possibly post some samples for Parent Delete Restrict ie. if the record I want to delete exists as a foreign key in a child table then don't perform the deletion.

Thanks,

Dave

Shuttleworth Business Systems

Development

All Replies

Posted by shuttleworth on 11-Jul-2012 06:06

It's small snippets of knowledge like THIS that really should be on the first page of the Java Trigger documentation.

I've now managed to create some simple trigger code instead of figuring out why the simplest of Java Triggers don't compile.

Posted by shuttleworth on 17-Jul-2012 08:27

This is what I ended up with after reading through all of the the stored procedure and triggers documentation.

CREATE TRIGGER kt1TimeTypeDelRest

BEFORE DELETE ON pub.kt1TimeType

REFERENCING OLDROW

FOR EACH ROW

IMPORT

import java.sql.*;

BEGIN

SQLCursor sqlc = new SQLCursor ("SELECT TimeTypeID FROM pub.kt1WorkingTime WHERE TimeTypeID = ?" );

sqlc.setParam(1, OLDROW.getValue(1,INTEGER));

sqlc.open();

sqlc.fetch();

if (sqlc.found())

{

                DhSQLException excep = new DhSQLException(1000,new String("TimeType used in WorkingTime"));

                sqlc.close();

                throw excep;

}

sqlc.close();

END

Posted by gus on 17-Jul-2012 09:19

Looks like you are checking to see if a row exists when doing an insert. Presumably most of the time the row will not exist so you have all this overhead that usually doesn't find anything on every insert.  Why don't you just let the insert fail and detect the error if there is a duplicate?

This thread is closed