[英]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.