简体   繁体   English

在表上触发PL / SQL插入数据

[英]Trigger pl/sql insert data on table

i'm new in pl/sql. 我是pl / sql的新手。 I'm trying to create a trigger that insert datas in specific tables. 我正在尝试创建一个在特定表中插入数据的触发器。 I have datas that arrives in real-time on my table EV_48h. 我的数据实时到达我的表EV_48h。 To know on which table I have to insert the data i have to know it Ref_equip (Ref_equip is on an other table named C_Equip). 要知道我必须在哪个表上插入数据,我必须知道Ref_equip(Ref_equip在另一个名为C_Equip的表上)。 I've made quickly this littre merise to be more understandable: merise 我迅速做了这个Littré酒店merise更加理解: merise

As I said I have data that arrives on real-time on the table EV_48H and I have to put them automatically on the tables that are named 'EVV_'+Ref_equip. 就像我说的那样,我的数据是实时到达表EV_48H上的,我必须将它们自动放在名为“ EVV _” + Ref_equip的表上。

So, here is my code. 所以,这是我的代码。 I don't have any error but it don't work. 我没有任何错误,但是没有用。 I know i missed of forget something but i don't know what. 我知道我错过了忘记某事,但我不知道该怎么办。

TRIGGER "SIVO"."NEWtrigger3EV_48H"
BEFORE INSERT
ON SIVO.EV_48H
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW

declare
clef_var number(4,0);
ref_equip varchar2(40);
V_Nom_table varchar2(1000) ;
V_nom_seq Varchar2(2000) ;
stmt varchar2(200);

begin
SELECT clef_var
INTO :New.Clef_Var
FROM sivo.c_variable
WHERE Ref_Var= :new.Ref_Var; 
  -- Conversion des formats Date-Heure en DateHeure oracle
  :New.EV_DATEAUTO := to_date(:New.EV_DATE || ' ' || :New.EV_HEURE, 'DD/MM/YY HH24:MI:SS');   
  stmt:='begin select clef_var into :New.Clef_Var From sivo.C_variable; end';
  EXECUTE IMMEDIATE stmt using out clef_var; 

  IF clef_var is not null then

    stmt :='begin select Ref_equip into :New.Ref_Equip FROM sivo.C_Equip WHERE Ref_var= :New.Ref_Var; end';
    EXECUTE IMMEDIATE Stmt USING OUT Ref_Equip;
    V_nom_table := 'EVV_'||Ref_Equip;
    stmt :='insert into' ||V_nom_table || '(:New.Clef_Var, :New.Ev_DateAuto, :New.Ev_Valeur )';
    EXECUTE IMMEDIATE stmt USING Ref_Equip; 

  ELSE
  INSERT INTO SIVO.EV_48H_VAR_INCONNUES (REF_VAR, EV_DATE, EV_HEURE, EV_VALEUR)
  VALUES ( :New.REF_VAR, :New.EV_DATE, :New.EV_HEURE, :New.EV_VALEUR);

  end if;
END;

If someone can help me or put me on the right way. 如果有人可以帮助我或以正确的方式帮助我。 I don't know if I give all informations so tell me if I missed something. 我不知道是否提供所有信息,所以告诉我是否错过了什么。 Thanks 谢谢

In your execute immediate you are missing the end of statement indicator colon after END this should be 在您的立即执行中,您缺少在END之后的语句指示符冒号结尾,这应该是

begin select clef_var into :New.Clef_Var From sivo.C_variable; end;

However there are other design choices that you should be aware of: 但是,您还应该注意其他设计选择:

  • using execute immediate is handy but if you don't have to use it you shouldn't. 使用立即执行很方便,但是如果您不必使用它,则不必这样做。 The work can be done by a cursor or even a simple select statement if only one value will come back. 如果仅返回一个值,则可以通过游标甚至是简单的select语句来完成工作。 In fact it appears you do the work twice. 实际上,您似乎完成了两次工作。 First you insert into the :new.clef_var then you do the same thing again with the execute immediate. 首先将您插入:new.clef_var,然后使用立即执行再次执行相同的操作。 Try commenting out the execute immediate. 尝试注释掉立即执行。
  • by using execute immediate any errors are harder to track 通过使用立即执行,更难跟踪任何错误
  • using a trigger means the real time data source cannot end the transaction until the trigger completes. 使用触发器意味着实时数据源在触发器完成之前无法结束事务。 Why not run a scheduled job every minute to check for new data and process it? 为什么不每分钟运行一次计划的作业以检查并处理新数据? This breaks the transaction into two parts: data entry and data processing 这将事务分为两部分:数据输入和数据处理
  • is there any update of records that your process needs to capture? 您的流程需要捕获的记录有没有更新?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM