简体   繁体   English

如果以前不存在要输入的值,如何创建SQL触发器以在另一个表中输入新值?

[英]How to create SQL Trigger to enter a new values into another table if value to be entered doesn't previously exist?

I'm working with Oracle SQL, and I have two tables. 我正在使用Oracle SQL,并且有两个表。 A table of conversations and a table of messages, and a message can't be added unless a conversation exists first. 对话表和消息表,除非首先存在对话,否则无法添加消息。 So when a new message wants to be added to the message table, I need to have a trigger to check is a conversation for that message exists yet or not, and if it doens't it will add a new conversation values to the conversation table for the message. 因此,当要将新消息添加到消息表中时,我需要触发一个触发器来检查该消息是否存在对话,如果不存在,它将在对话表中添加新的对话值消息。 "Warning: Trigger created with compilation errors." “警告:触发器因编译错误而创建。” But I don't seem to see my error. 但是我似乎看不到我的错误。 I was hoping to get any insight here on what I can do to make this code work better. 我希望在这里可以做些什么使我的代码更好地工作的见解。

CREATE OR REPLACE TRIGGER CreateConversation
    BEFORE INSERT ON Messages
    FOR EACH ROW
    BEGIN
        dbms_output.put_line('made it inside');
        IF NOT EXISTS (select * FROM Conversation WHERE convID =     new.convID AND msgID = new.msgID ) 
            THEN
            dbms_output.put_line('Conversation does not exist!');
            INSERT INTO Conversation VALUES(:new.convID, :new.msgID);
        END IF;
    END;
/

You can not use EXISTS in PL/SQL; 您不能在PL / SQL中使用EXISTS if you need to check the existence of related records, try with something like this (not tested): 如果您需要检查相关记录的存在,请尝试以下类似方法(未经测试):

CREATE OR REPLACE TRIGGER CreateConversation 
    BEFORE INSERT ON Messages
    FOR EACH ROW
declare    
    vCount number;
    BEGIN
        dbms_output.put_line('made it inside');
        select count(1) 
        into vCount
        FROM Conversation
        WHERE convID = :new.convID
          AND msgID = :new.msgID;
        IF vCount = 0
            THEN
            dbms_output.put_line('Conversation does not exist!');
            INSERT INTO Conversation VALUES(:new.convID, :new.msgID);
        END IF;
    END;
/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL触发器:如果在第一个表中不存在给定值,则在另一个表中的插入之后在表中插入行 - SQL Trigger: Inserting a row in a table after an insert on another table, if a given value doesn't already exist in the first table 仅在表中不存在值时如何激活SQL触发器? - How to activate a SQL trigger only when a value doesn't exist in the table? 用于检查另一个表中是否不存在值的SQL约束 - SQL constraint to check whether value doesn't exist in another table 如何在表中选择一个值,如果它不存在另一个值? - How to select a value in a table and if it doesn't exist another value? .NET SQL新表不存在 - .NET SQL new table doesn't exist 仅当 sql 的表中不存在值时如何检查和插入值 - How to check and insert values only if it doesn't exist in the table for sql SQL Server:创建触发器以将旧值替换为另一个表上的新值 - SQL Server : create trigger to replace old value to new value on another table 是否可以编写一个 SQL 查询来返回另一个表中不存在的值? - Is it possible to write an SQL query that returns the values of something that doesn't exist in another table? 仅当数据库中尚不存在同名表时,如何将表创建为另一个表? - How can I create a table as another table, only if the table by the same name doesn't yet exist in a database? 如果表不存在,如何创建*并填充*表? - How to create *and populate* a table if it doesn't exist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM