0

我想创建一个触发器,该触发器在更新表中的行之前运行,并在将行中的所有字段更新到存档表之前写入该行中的所有字段。在更新之前访问行字段所需的正确语法是什么,以便我可以将它们写入我的存档表?

编辑 :

所以这应该做我想要的,但它似乎不起作用。我收到错误“数据库中已经有一个名为 config_SystemSettings 的对象:

CREATE TRIGGER [config].[UpdateSystemSettings]

ON [config].[SystemSetting] 更新开始后

SET NOCOUNT ON;

SELECT old.settingId, old.campId, old.settingKey, old.settingValue
into [history].[config_SystemSettings]
FROM [config].[SystemSetting] AS old
INNER JOIN deleted AS del ON del.settingId = old.settingId

结束

4

1 回答 1

0

SELECT ... INTO总是想创建一个新表 - 所以INSERT ... SELECT改用:

CREATE TRIGGER [config].[UpdateSystemSettings]

ON [config].[SystemSetting] AFTER UPDATE AS BEGIN
SET NOCOUNT ON;

insert into [history].[config_SystemSettings] (settingId,campId,settingKey,settingValue)
SELECT old.settingId, old.campId, old.settingKey, old.settingValue
FROM [config].[SystemSetting] AS old
INNER JOIN deleted AS del ON del.settingId = old.settingId

但是您必须先明确创建[history].[config_SystemSettings]

于 2012-04-13T09:05:33.670 回答