如何在Firebird中使用触发器中的游标进行删除?

时间:2017-08-30 12:21:03

标签: sql cursor firebird database-trigger

我尝试在Firebird中编写一个触发器,以便在从表中删除记录时自动删除其他表中的记录。就像通过级联删除一样。出于某些原因,有些表没有主键。

触发器就像这样:

CREATE TRIGGER TRG1_DEL_STEP_INFO FOR STEP_INFO
ACTIVE AFTER DELETE
AS
DECLARE VARIABLE del_ID_IMAGE_INFO bigint;
DECLARE VARIABLE del_ID_FRAME_INFO bigint;

DECLARE del_cursor CURSOR FOR (SELECT ID_IMAGE_INFO, ID_FRAME_INFO FROM OLD);

    BEGIN

       OPEN del_cursor;

       WHILE (ROW_COUNT > 0) DO

         BEGIN

           FETCH del_cursor INTO del_ID_IMAGE_INFO, del_ID_FRAME_INFO;

             DELETE FROM FRAME_POLYGON WHERE ID_FRAME_INFO = :del_ID_FRAME_INFO;

             DELETE FROM FRAME_INFO WHERE RECID = :del_ID_FRAME_INFO;

             DELETE FROM IMAGE_INFO WHERE RECID = :del_ID_IMAGE_INFO;

             DELETE FROM IMAGE_FILE_INFO WHERE ID_IMAGE_INFO = :del_ID_IMAGE_INFO;

          END

         CLOSE del_cursor;
    END

但是当我试图通过isql将它放入我的Firebird DB时,我收到了这样的错误:

Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-OLD
-At line 6, column 73

请您告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您不能使用光标,也不需要光标。 Firebird中的触发器每行触发,而不是按语句触发。

所以你应该这样做:

CREATE TRIGGER TRG1_DEL_STEP_INFO FOR STEP_INFO
ACTIVE AFTER DELETE
AS
BEGIN
    DELETE FROM FRAME_POLYGON WHERE ID_FRAME_INFO = OLD.ID_FRAME_INFO;
    DELETE FROM FRAME_INFO WHERE RECID = OLD.ID_FRAME_INFO;
    DELETE FROM IMAGE_INFO WHERE RECID = OLD.ID_IMAGE_INFO;
    DELETE FROM IMAGE_FILE_INFO WHERE ID_IMAGE_INFO = OLD.ID_IMAGE_INFO
END