创建审核触发器时抛出警告作为编译错误

发布于 2024-12-22 06:28:45 字数 970 浏览 1 评论 0原文

我尝试创建一个审核触发器,它会抛出编译错误。

你能帮我创建触发器吗?

DROP TRIGGER DB.DAT_CAMPLE_REQ_Test;

CREATE OR REPLACE TRIGGER DB."DAT_CAMPLE_REQ_Test"
AFTER insert or update or delete on DAT_CAMPLE_REQ
FOR EACH ROW  
declare
  dmltype varchar2(6);
BEGIN
  if deleting then

    INSERT INTO h_dat_cample_req VALUES     (       
      :Old.REQUEST_ID,      
      :Old.SAMPLE_ID,      
      :Old.CASSAY_ID,       
      :Old.CASCADE_ID,       
      :Old.STATUS_ID,       
      :Old.AUTHOR,       
      :Old.CRT_SAE,       
      :Old.SCREEN_SAE
    ); 

  else

    if inserting then   

      dmltype := 'insert';  

    elsif updating then 

      dmltype := 'update';   

    end if;  

    INSERT INTO h_dat_cample_req VALUES
    (
      :New.REQUEST_ID,  
      :New.SAMPLE_ID,  
      :New.CASSAY_ID,  
      :New.CASCADE_ID, 
      :New.STATUS_ID,  
      :New.AUTHOR,   
      :New.CRT_SAE,     
      :New.SCREEN_SAE 
     ); 

  end if; 

END; 

I try to create a audit trigger it throwing compilation error.

could you please help me for creating trigger..

DROP TRIGGER DB.DAT_CAMPLE_REQ_Test;

CREATE OR REPLACE TRIGGER DB."DAT_CAMPLE_REQ_Test"
AFTER insert or update or delete on DAT_CAMPLE_REQ
FOR EACH ROW  
declare
  dmltype varchar2(6);
BEGIN
  if deleting then

    INSERT INTO h_dat_cample_req VALUES     (       
      :Old.REQUEST_ID,      
      :Old.SAMPLE_ID,      
      :Old.CASSAY_ID,       
      :Old.CASCADE_ID,       
      :Old.STATUS_ID,       
      :Old.AUTHOR,       
      :Old.CRT_SAE,       
      :Old.SCREEN_SAE
    ); 

  else

    if inserting then   

      dmltype := 'insert';  

    elsif updating then 

      dmltype := 'update';   

    end if;  

    INSERT INTO h_dat_cample_req VALUES
    (
      :New.REQUEST_ID,  
      :New.SAMPLE_ID,  
      :New.CASSAY_ID,  
      :New.CASCADE_ID, 
      :New.STATUS_ID,  
      :New.AUTHOR,   
      :New.CRT_SAE,     
      :New.SCREEN_SAE 
     ); 

  end if; 

END; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2024-12-29 06:28:45

您没有提供确切的错误消息,也没有提供表 h_dat_cample_req 的结构,所以恐怕我不得不猜测。

我怀疑您的 h_dat_cample_req 中的列名称未按您预期的顺序排列,或者表中还有其他列您尚未在 INSERT 中指定值声明。

您使用的是 INSERT 语句,但没有列出每个值应进入的列。使用这种形式的 INSERT 语句的问题是,如果表中的列的顺序与您认为的顺序不完全一致,或者存在已添加或删除的列,您将一旦出现错误,就很难追踪到它。此外,如果没有出现编译错误,数据仍有可能被插入到错误的列中。命名列可以清楚地表明哪个值属于哪个列,可以更轻松地识别已删除的列,并且还意味着您不必为表中的所有列指定值 - 任何未列出的列都会得到一个NULL值。

我强烈建议始终在 INSERT 语句中命名列。换句话说,您不是编写

INSERT INTO some_table VALUES (value_1, value_2, ...);

write

INSERT INTO some_table (column_1, column_2, ...) VALUES (value_1, value_2, ...);

顺便说一句,而是为变量 dmltype 分配一个值,但您没有在任何地方使用它的值。这不会导致编译错误,但它表明您的触发器可能没有完全按照您的预期进行操作。也许您的 h_dat_cample_req 表是一个历史表,并且有一列表示所执行操作的类型?

You haven't provided the exact error message nor the structure of the table h_dat_cample_req, so I'm afraid I'm going to have to guess.

I suspect the column names in your h_dat_cample_req are not in the order you expect, or there are other columns in the table that you haven't specified a value for in your INSERT statements.

You are using INSERT statements without listing the columns that each value should go in to. The problem with using this form of INSERT statement is that if the columns in the table aren't in exactly the order you think they are, or there are columns that have been added or removed, you'll get an error and it'll be difficult to track it down. Furthermore, if you don't get a compilation error there's still the chance that data will be inserted into the wrong columns. Naming the columns makes it clear which value goes in which column, makes it easier to identify columns that have been removed, and also means that you don't have to specify values for all of the columns in the table - any column not listed gets a NULL value.

I would strongly recommend always naming columns in INSERT statements. In other words, instead of writing

INSERT INTO some_table VALUES (value_1, value_2, ...);

write

INSERT INTO some_table (column_1, column_2, ...) VALUES (value_1, value_2, ...);

Incidentally, you're assigning a value to your variable dmltype but you're not using its value anywhere. This won't cause a compilation error, but it is a sign that your trigger might not be doing quite what you would expect it to. Perhaps your h_dat_cample_req table is a history table and has a column for the type of operation performed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文