2016-04-01 94 views
0

我经历了以前的答案,创建了一个伪外键来引用Netbeans 8.1中两个数据库之间的表。这是我想出了一个代码,两个数据库之间的外键

DELIMITER // 

CREATE OR REPLACE TRIGGER conf_track_FK 
AFTER INSERT OR UPDATE on [email protected] 
FOR EACH ROW 
BEGIN 
    IF EXISTS(select * from inserted I where not exists (select * from 
    [email protected] A where I.conf_id=A.conf_id)) 
    RAISE_APPLICATION_ERROR(-20001,'Violation of pseudo-foreign key.'); 
    ROLLBACK; 
    END IF; 
END; 

/

不过,我遇到了以下错误:

PLS-00103: Encountered the symbol ";" when expecting one of the following: 
    ) with and or group having intersect minus start union where 
    connect 

PLS-00103: Encountered the symbol "ROLLBACK" when expecting one of the following: 
    := . (% ; 
The symbol ":=" was substituted for "ROLLBACK" to continue. 

PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: 

    end not pragma final instantiable order overriding static 
    member constructor map 
+1

您的错误消息表明Oracle和'@ FIT5148B'是DBLink。您无法通过数据库链接创建触发器。您需要连接到远程数据库并在那里创建触发器。另外:对于我所知的任何Oracle SQL工具,''delimiter //'都是无效的。你确定你的工具支持吗?此外,在Oracle中不存在“插入”这样的事情 - 特别是不在行级触发器中。 –

+0

另外'if-statement'结构显然是错误的。请先帮助自己并阅读一些基本教程。他们不是很难[找](http://stackoverflow.com/tags/plsql/info)。 – user272735

回答

0

创建触发器dbo.MyTableTrigger ON dbo.MyTable,插入,更新 后作为 开始

如果不存在(从OtherDB.dbo.TableName中选择PK,其中PK in(从插入中选择FK)BEGIN - 在此处理参考错误 END

END

+0

嗨,感谢您的快速回复,这是我试图遵循的基本结构。但是,错误处理似乎是导致错误的部分。我无法解决这个问题。 –

0

请尝试下面的插图代码段。希望能帮助到你。并且像COMMIT/ROLLBACK这样的任何TRANSACTIONS都不能被放置在INSED触发器中,除非它是一个自治事务,但是对于触发器事务来说,父事务并不是一个好主意。因此,即使父交易失败,Automnomous交易也会完成。

CREATE OR REPLACE TRIGGER conf_track_FK AFTER 
    INSERT OR 
    UPDATE ON [email protected] --Remove DB link as it cant be used in Trigger 
    FOR EACH ROW 
    DECLARE 
    lv_cnt PLS_INTEGER; 
    BEGIN 
    SELECT COUNT(1) 
    INTO lv_cnt 
    FROM inserted I 
    WHERE NOT EXISTS 
     (SELECT 1 FROM [email protected] A WHERE I.conf_id=A.conf_id 
    ); 
    IF lv_cnt > 0 THEN 
     RAISE_APPLICATION_ERROR(-20001,'Violation of pseudo-foreign key.'); 
    END IF; 
    END; 
/
相关问题