【发布时间】:2015-03-10 06:34:30
【问题描述】:
我正在尝试在 SQL Server 数据库上实现一个触发器,只要在 table1 中更新一行,它就会在 table2 中插入一个新行。我想在更新前获取行数据(table1)。
例如,我有以下几行:
table1: table2:
________________________ ________________________
| id | name | | id | name |
________________________ ________________________
| 1 | John |
------------------------
目前我正在使用以下触发器-
CREATE TRIGGER triggername
ON table1
FOR UPDATE
AS
Begin
INSERT INTO table2(name)
SELECT i.name
FROM Inserted i
End
此触发器正在执行以下操作 -
table1: table2:
________________________ ________________________
| id | name | | id | name |
________________________ ________________________
| 1 | Alex | | 1 | Alex |
------------------------ ------------------------
但我想要 table2 中的“John”。比如table1和table2现在应该如下-
table1: table2:
________________________ ________________________
| id | name | | id | name |
________________________ ________________________
| 1 | Alex | | 1 | John |
------------------------ ------------------------
我该怎么做?
【问题讨论】:
标签: sql sql-server triggers