Database 创建一个插入触发器
当某些数据插入到另一个表中时,如何在SQL Server中编写触发器来帮助我将值插入到表中?我还想使用第二个表中插入的数据来插入第一个表 有表A和表B 在表B中插入行时(TBLP1ISLEM) 我想从这个插入中插入几个单元格 进入表A(TBLP1DOVIZKURU) 插入表B 触发器帮助=>插入的表A 多谢各位 代码Database 创建一个插入触发器,database,sql-server-2008,triggers,Database,Sql Server 2008,Triggers,当某些数据插入到另一个表中时,如何在SQL Server中编写触发器来帮助我将值插入到表中?我还想使用第二个表中插入的数据来插入第一个表 有表A和表B 在表B中插入行时(TBLP1ISLEM) 我想从这个插入中插入几个单元格 进入表A(TBLP1DOVIZKURU) 插入表B 触发器帮助=>插入的表A 多谢各位 代码 在表B上为insert添加触发器。它将允许您捕获插入的数据并对其执行任何操作。您可以使用插入的数据,也可以查询其他表。。。等等 CREATE TRIGGER trigger_Ins
在表B上为insert添加触发器。它将允许您捕获插入的数据并对其执行任何操作。您可以使用插入的数据,也可以查询其他表。。。等等
CREATE TRIGGER trigger_Insert_TableB
ON TableB
FOR INSERT
AS
Begin
Select * FROM Inserted //This will give you the inserted data
End
我写了一个完全一样的代码,但它不工作,我想知道你为什么会得到任何具体的错误?可能表A上还有其他一些触发器不允许数据插入,例如PK、FK、约束、null或其他触发器。谢谢,我试过了,但没有成功,表可能有其他问题。
CREATE TRIGGER trigger_Insert_TableB
ON TableB
FOR INSERT
AS
Begin
Select * FROM Inserted //This will give you the inserted data
End
CREATE TRIGGER your_trigger_name
ON [TableB]
FOR INSERT
AS
BEGIN
INSERT INTO TableA (tA_field1,tA_field2,tA_other_fields) SELECT tB_field1,tB_field2,tB_other_fields FROM Inserted
END
SYSTEM@valeh> create table emp_list (id number, name varchar2(20));
SYSTEM@valeh> insert into emp_list values (1,'Valeh');
SYSTEM@valeh> create table emp_age (age_id number, emp_id number, emp_age number);
SYSTEM@valeh> create view emp_view as select * from emp_list el,emp_age age where el.id=age.emp_id;
create or replace trigger view_trigger
instead of insert on emp_view
referencing new as new old as old
declare
v_id VARCHAR(20);
begin
if :new.id is not null then
insert into emp_list (id,name) values (:new.id,:new.name);
insert into emp_age (age_id,emp_id,emp_age) values (:new.age_id,:new.emp_id,:new.emp_age)
returning :new.id into v_id;
else
raise_application_error (-20999, 'Cannot create employee without name');
end if;
end;
SYSTEM@valeh> insert into emp_view values (2,'taleh',2,2,20);
SYSTEM@valeh> select * from emp_view;