简体   繁体   English

SQL Server:如果在一个表中插入一行,我如何编写触发器以在不同的表中插入同一行?

[英]SQL Server: If a row is inserted in one table, how do I write a trigger to insert that same row in a different table?

I'm not sure how to create this trigger,.I need to add a row in pub_info table when a row is inserted in publishers table.我不确定如何创建这个触发器。当在publishers 表中插入一行时,我需要在pub_info 表中添加一行。 The exact same row.完全相同的行。 SQL server SQL服务器

CREATE TRIGGER checkCity
ON pub_info
AFTER INSERT
AS
IF -- a row is inserted into publishers table, 
   -- how do I add the same row into pub_info table?
INSERT VALUES(@pub_id, NULL, 'new publishers')
BEGIN
END;

You have to create a trigger on publishers table and not on pub_info .您必须在publishers表上而不是在pub_info上创建触发器。 The inserted data is available in the INSERTED table in the trigger插入的数据在触发器的INSERTED表中可用

CREATE TRIGGER checkCity
ON publishers
AFTER INSERT
AS
BEGIN
  INSERT INTO pub_info(pubid, pubname, pub_description)
      SELECT pubid, pubname, pub_description 
      FROM INSERTED;
END;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL-SERVER 程序如何在一个表中插入行,然后将行插入到另一个表中作为外键链接到另一个表 - SQL-SERVER Procedure How do i insert row one table then insert row into another table link to another table as foreign key 我如何从一个表中选择同一行为另一张表中的不同值+ SQL - how do i select the same row from one table for different values in another table + sql INSERT可以在触发器“插入”表中产生多行吗? - Can an INSERT result in more than one row in a trigger “inserted” table? 如何在sql中的一个表中插入一行,该行与另一个表完全相同但只有一列不同? - How to insert a row into one table in sql that is exact same as another table but only one column different? 在mysql中通过触发器删除插入表中的同一行 - Delete the same row inserted in a table by trigger in mysql 如何编写一个查询,将一个表的每一行与 MySQL 中不同表的所有行进行比较? - How do I write a query that compares each row of one table to all the rows of a different table in MySQL? 如何创建SQL触发器以根据新插入的行中的值插入新行? - How do I create a SQL trigger to insert new rows based on a value in a newly inserted row? 我们可以插入一张表并通过查看其他表的值来更新插入行的一列吗? - Can we insert in one table and trigger to update the inserted row's one column by looking at other table's value for the id being inserted? Sql Server触发器将值从新行插入另一个表 - Sql Server trigger insert values from new row into another table (API)如何在多个表格中插入行 - (API) How do I insert row in more than one table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM