CREATE触发器帮助Oracle

发布于 2024-09-10 10:11:02 字数 402 浏览 2 评论 0原文

我正在尝试创建一个触发器来执行以下操作。

在表 A 上插入后,根据 id (TableA.id=TableB.id) 查询表 B 并将相应的信息插入表 A

我有一种感觉,到目前为止我还很遥远,所以任何帮助将不胜感激

CREATE OR REPLACE TRIGGER myTrig
AFTER INSERT
ON TABLEA
BEGIN
  INSERT INTO TABLEA
  SELECT TABLEB.FIRST_NAME, TABLEB.LAST_NAME, SYSDATE
  FROM TABLEA JOIN TABLEB ON 
  TABLEA.STUDENT_ID=TABLEB.STUDENT_ID
  insert into TABLEA values (....);
END;

I'm trying to create a trigger that will do the following.

After insert on Table A, query Table B based on an id (TableA.id=TableB.id) and insert corresponding info into TableA

I have a feeling I'm way off so far so any help would be appreciated

CREATE OR REPLACE TRIGGER myTrig
AFTER INSERT
ON TABLEA
BEGIN
  INSERT INTO TABLEA
  SELECT TABLEB.FIRST_NAME, TABLEB.LAST_NAME, SYSDATE
  FROM TABLEA JOIN TABLEB ON 
  TABLEA.STUDENT_ID=TABLEB.STUDENT_ID
  insert into TABLEA values (....);
END;

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

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

发布评论

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

评论(1

时光与爱终年不遇 2024-09-17 10:11:02

这是行不通的。想想你在问什么。您需要一个在将行插入 tableA 时触发的触发器,以将行插入 tableA。扳机什么时候会停止射击?

Oracle 足够聪明,可以介入并防止触发器陷入无限:

SQL> create or replace trigger t69_after_ins
  2      after insert on t69
  3  begin
  4      insert into t69 values ('blah', 'blah', 99);
  5  end;
  6  /

Trigger created.

SQL>

以下是发生的情况:

SQL> insert into t69 values ('this', 'that', 1)      
   2  /

insert into t69 values ('this', 'that', 1)
            *
ERROR at line 1:
ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_I


SQL>

“有什么办法可以更新
表A基于表B给出的信息
两个表中都有的 id 吗? ”

如果“更新”实际上意味着更新,并且 - 至关重要的是 - 取决于您希望实现的精确逻辑,那么也许是的:

SQL> create or replace trigger t69_after_ins
  2      after insert on t69
  3  begin
  4      update t69
  5          set name = ( select name from t23
  6                       where t23.id = t69.id )
  7          where name is null;
  8  end;
  9  /

Trigger created.

SQL> insert into t69 (id, name) values (122, null)
  2  /

1 row created.

SQL> select name from t69
  2  where id = 122
  3  /

NAME
----------
MAISIE

SQL>

仍然是一个坏主意。触发器很难理解,并且可能对性能产生有害影响因此,我建议您尝试找出一种将逻辑构建到应用程序主体中的方法,而不是尝试使用触发器。

This will not work. Just think about what you are asking. You want a trigger that fires when a row is inserted into tableA to insert a row into tableA. When would the trigger stop firing?

Oracle is smart enough to step in and prevent the trigger spiralling into infinity:

SQL> create or replace trigger t69_after_ins
  2      after insert on t69
  3  begin
  4      insert into t69 values ('blah', 'blah', 99);
  5  end;
  6  /

Trigger created.

SQL>

Here's what happens:

SQL> insert into t69 values ('this', 'that', 1)      
   2  /

insert into t69 values ('this', 'that', 1)
            *
ERROR at line 1:
ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_INS'
ORA-06512: at "APC.T69_AFTER_INS", line 2
ORA-04088: error during execution of trigger 'APC.T69_AFTER_I


SQL>

"Is there any way that I can update
TableA based on info from TableB given
an id that is in both tables? "

If by "update" you actually do mean UPDATE, and - crucially - depending on the precise logic you wish to implement then perhaps yes:

SQL> create or replace trigger t69_after_ins
  2      after insert on t69
  3  begin
  4      update t69
  5          set name = ( select name from t23
  6                       where t23.id = t69.id )
  7          where name is null;
  8  end;
  9  /

Trigger created.

SQL> insert into t69 (id, name) values (122, null)
  2  /

1 row created.

SQL> select name from t69
  2  where id = 122
  3  /

NAME
----------
MAISIE

SQL>

However this remains a bad idea. Triggers are hard to understand and can have a deleterious nimpact on the performance of our SQL. So I suggest you try to figure out a way of building your logic into the main body of your application, rather than trying to use a trigger.

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