多表触发器

发布于 2024-10-23 14:53:43 字数 1272 浏览 6 评论 0原文

我正在尝试创建一个触发器,该触发器将从一个表中获取一个值,然后与另一个表中的其他值进行减法。

触发器将在表(存款)中操作,然后我想获取用户刚刚插入存款表的值,并用另一个表(帐户)中的值减去它。我可以毫无问题地编译以下代码:

CREATE OR REPLACE TRIGGER aifer_insättning 
AFTER INSERT
ON deposit
FOR EACH ROW
WHEN (new.belopp is not null)
BEGIN
UPDATE account SET SALDO = saldo + :new.belopp;
end;
/

但是当然,这会更新表帐户中的所有行。我已尝试一切方法使其仅适用于一行,但我无法得到它。有人有什么建议我可以解决这个问题吗? 这是我的表格:

SQL> desc account
 Name                                      Null?    Type
 ----------------------------------------- -------- ------------
 KNR                                       NOT NULL NUMBER(8)
 KTNR                                      NOT NULL NUMBER(6)
 REGDATUM                                  NOT NULL DATE
 SALDO                                              NUMBER(10,2)

SQL> desc deposit
 Name                                      Null?    Type
 ----------------------------------------- -------- ------------
 RADNR                                     NOT NULL NUMBER(9)
 PNR                                       NOT NULL VARCHAR2(11)
 KNR                                       NOT NULL NUMBER(8)
 BELOPP                                             NUMBER(10,2)
 DATUM                                     NOT NULL DATE

I am trying to make a trigger that are going to take a value from one table and then make a subtraction with an other value in another table.

The trigger are going to operate in a table could (deposit), and then I would like to take the value that the user just inserted to the deposit table and subtract it with a value in another table(account). I have no problem to get following code to compile:

CREATE OR REPLACE TRIGGER aifer_insättning 
AFTER INSERT
ON deposit
FOR EACH ROW
WHEN (new.belopp is not null)
BEGIN
UPDATE account SET SALDO = saldo + :new.belopp;
end;
/

But of course this update al the rows in the table account. I have tried everything to make it work with only one row, but I can´t get it. Does anyone have some suggestion hove I can resolve this?
Here is my tables:

SQL> desc account
 Name                                      Null?    Type
 ----------------------------------------- -------- ------------
 KNR                                       NOT NULL NUMBER(8)
 KTNR                                      NOT NULL NUMBER(6)
 REGDATUM                                  NOT NULL DATE
 SALDO                                              NUMBER(10,2)

SQL> desc deposit
 Name                                      Null?    Type
 ----------------------------------------- -------- ------------
 RADNR                                     NOT NULL NUMBER(9)
 PNR                                       NOT NULL VARCHAR2(11)
 KNR                                       NOT NULL NUMBER(8)
 BELOPP                                             NUMBER(10,2)
 DATUM                                     NOT NULL DATE

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

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

发布评论

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

评论(4

橘寄 2024-10-30 14:53:43

假设公共列KNR是ACCOUNT的主键,我认为你想要的代码是:

CREATE OR REPLACE TRIGGER aifer_insättning 
    AFTER INSERT
    ON deposit
    FOR EACH ROW
    WHEN (new.belopp is not null)
BEGIN
    UPDATE account a
    SET a.SALDO = a.saldo + :new.belopp
    WHERE a.knr - :new.knr;
end;
/

注意,我添加了表别名以使代码更清晰。

Presuming that the common column KNR is the primary key of ACCOUNT, I think the code you want is:

CREATE OR REPLACE TRIGGER aifer_insättning 
    AFTER INSERT
    ON deposit
    FOR EACH ROW
    WHEN (new.belopp is not null)
BEGIN
    UPDATE account a
    SET a.SALDO = a.saldo + :new.belopp
    WHERE a.knr - :new.knr;
end;
/

Note that I have added a table alias to make the code clearer.

花开浅夏 2024-10-30 14:53:43

您不需要在触发器主体中的 update 语句中添加一个 where 子句吗?或者我错过了什么?

Wouldn't you just need to add a where clause to the update statement in the trigger body? Or am I missing something?

秋日私语 2024-10-30 14:53:43

分隔符 $$
在更新员工之前创建触发器 ss_ss
对于每一行
开始
更新收货人设置
创建者 = NEW.employee_pin
其中created_by = OLD.employee_pin;
结束
$$
分隔符;

NOTE->员工和收货人是两个表

delimiter $$
create trigger ss_ss before update on employee
for each row
begin
update consignee set
created_by = NEW.employee_pin
where created_by = OLD.employee_pin;
end
$$
delimiter ;

NOTE-> employee and consignee are two tables

匿名的好友 2024-10-30 14:53:43

**

为所有表生成触发器

**
嗯,我最初这样做是为了为数据库中的所有表生成触发器来审计数据更改,这很简单,只需将整个行从已删除的表移动到镜像审计表即可。

但有人想要跟踪桌子上的活动,所以它更简单一些。这里我们创建一个日志表,任何时候发生DML操作,都会将其写入其中。

享受

USE Northwind GO

CREATE TABLE LOG_TABLE (
  Add_dttm datetime DEFAULT (GetDate()),
  TABLE_NAME sysname,
  Activity char(6)
);

GO

DECLARE
  @sql varchar(8000),
  @TABLE_NAME sysname SET NOCOUNT ON

SELECT @TABLE_NAME = MIN(TABLE_NAME)
FROM INFORMATION_SCHEMA.Tables

WHILE @TABLE_NAME IS NOT NULL
BEGIN
  SELECT @sql = 'CREATE TRIGGER
  [' + @TABLE_NAME + '_Usage_TR]
    ON [' + @TABLE_NAME +'] '
    + 'FOR INSERT, UPDATE, DELETE AS '
    + 'IF EXISTS (SELECT * FROM inserted)
  AND NOT EXISTS (SELECT * FROM deleted) '
  + 'INSERT INTO LOG_TABLE
  (TABLE_NAME,Activity)
  SELECT ''' + @TABLE_NAME + ''', ''INSERT''' + ' '
  + 'IF EXISTS (SELECT * FROM inserted)
  AND EXISTS (SELECT * FROM deleted) '
  + 'INSERT INTO LOG_TABLE (TABLE_NAME,Activity) SELECT '''
  + @TABLE_NAME + ''', ''UPDATE''' + ' '
  + 'IF NOT EXISTS (SELECT * FROM inserted)
  AND EXISTS (SELECT * FROM deleted) '
  + 'INSERT INTO LOG_TABLE (TABLE_NAME,Activity)
  SELECT ''' + @TABLE_NAME + ''',
  ''DELETE''' + ' GO'

  SELECT @sql EXEC(@sql)

  SELECT @TABLE_NAME = MIN(TABLE_NAME)
  FROM INFORMATION_SCHEMA.Tables
  WHERE TABLE_NAME > @TABLE_NAME
END
SET NOCOUNT OFF

**

Generate Triggers for all Tables

**
Well, I did this originally to generate triggers for all tables in a database to audit data changes, and that is simple enough, just move the entire row from the deleted table to a mirrored audit table.

But someone wanted to track activity on tables, so it's a little more simple. Here we create one log table, and any time a DML operation occurs, it is written there.

Enjoy

USE Northwind GO

CREATE TABLE LOG_TABLE (
  Add_dttm datetime DEFAULT (GetDate()),
  TABLE_NAME sysname,
  Activity char(6)
);

GO

DECLARE
  @sql varchar(8000),
  @TABLE_NAME sysname SET NOCOUNT ON

SELECT @TABLE_NAME = MIN(TABLE_NAME)
FROM INFORMATION_SCHEMA.Tables

WHILE @TABLE_NAME IS NOT NULL
BEGIN
  SELECT @sql = 'CREATE TRIGGER
  [' + @TABLE_NAME + '_Usage_TR]
    ON [' + @TABLE_NAME +'] '
    + 'FOR INSERT, UPDATE, DELETE AS '
    + 'IF EXISTS (SELECT * FROM inserted)
  AND NOT EXISTS (SELECT * FROM deleted) '
  + 'INSERT INTO LOG_TABLE
  (TABLE_NAME,Activity)
  SELECT ''' + @TABLE_NAME + ''', ''INSERT''' + ' '
  + 'IF EXISTS (SELECT * FROM inserted)
  AND EXISTS (SELECT * FROM deleted) '
  + 'INSERT INTO LOG_TABLE (TABLE_NAME,Activity) SELECT '''
  + @TABLE_NAME + ''', ''UPDATE''' + ' '
  + 'IF NOT EXISTS (SELECT * FROM inserted)
  AND EXISTS (SELECT * FROM deleted) '
  + 'INSERT INTO LOG_TABLE (TABLE_NAME,Activity)
  SELECT ''' + @TABLE_NAME + ''',
  ''DELETE''' + ' GO'

  SELECT @sql EXEC(@sql)

  SELECT @TABLE_NAME = MIN(TABLE_NAME)
  FROM INFORMATION_SCHEMA.Tables
  WHERE TABLE_NAME > @TABLE_NAME
END
SET NOCOUNT OFF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文