创建插入触发器

发布于 2024-10-30 22:14:02 字数 523 浏览 3 评论 0原文

如何在 SQL Server 中编写一个触发器,当将某些数据插入另一个表时,它可以帮助我将值插入到一个表中?而且我还想使用第二个表中插入的数据来插入第一个表。

有表 A 和表 B,

当将一行插入表 B(TBLP1ISLEM) 时,

我想将此插入中的多个单元格插入

表 A(TBLP1DOVIZKURU) 中。

表B,插入

触发器帮助=>表A,插入

谢谢。

代码

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

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

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

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

发布评论

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

评论(3

无所的.畏惧 2024-11-06 22:14:03

在表 B 上添加触发器以进行插入。它将允许您捕获插入的数据并用它做任何您想做的事情。你可以使用插入的数据,你可以查询另一个表......等等......

CREATE TRIGGER trigger_Insert_TableB
ON TableB
FOR INSERT
AS
Begin
    Select * FROM Inserted //This will give you the inserted data
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
玩套路吗 2024-11-06 22:14:03
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
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
你在我安 2024-11-06 22:14:03
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;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文