create an insert trigger
How can I write a trigger in SQL Server that will help me to insert values开发者_运维百科 into a table when some data is inserted into another table? And I also want to use the inserted data in the second table to insert the first table.
There is Table A and Table B,
when a row is inserted into Table B(TBLP1ISLEM)
I want to insert several cells from this insertion,
into Table A(TBLP1DOVIZKURU).
Table B, inserted
Trigger help => Table A, inserted
Thank you.
code
CREATE TRIGGER [dbo].[Deneme]
ON [dbo].[TBLP1ISLEM]
FOR INSERT
AS
IF(SELECT ID FROM inserted)>0
BEGIN
SET NOCOUNT ON;
INSERT INTO TBLP1DOVIZKURU(ISLEM_ID,DOVIZBIRIM,KUR)
SELECT ID, PARABIRIMI, KUR FROM inserted
END
Add a trigger on Table B for insert. It will allow you to capture the inserted data and do whatever you want with it. You can use the inserted data, you can query another table ... etc ...
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;
精彩评论