SQL 触发器 - 删除还是更新?或者也许是别的什么?
我想弄清楚我需要在这里使用哪个:删除、插入或更新。
基本上。
当主表更新时,并且仅当状态从某项更改为挂起或活动时,我需要将一些数据写入历史表。
这就是我现在所拥有的:
ALTER TRIGGER [dbo].[trg_SourceHistory] ON [dbo].[tblSource]
FOR UPDATE AS
DECLARE @statusOldValue char(1)
DECLARE @statusNewValue char(1)
SELECT @statusOldValue = statusCode FROM deleted
SELECT @statusNewValue= statusCode FROM updated
IF (@statusOldValue <> @statusNewValue) AND
(@statusOldValue = 'P' or @statusOldValue = 'A')
BEGIN TRY
INSERT * INTO tblHistoryTable)
select * from [DELETED]
所以我希望新数据保留在主表中,历史表要使用被覆盖的内容进行更新......现在它只是复制相同的信息。所以更新后,我的两个表都有相同的数据。
I am trying to figure out which i need to use here: deleted, inserted or updated.
basically.
I need to write some data to the history table, when the main table is updated, and only if the status changes from something to either pending or active.
This is what I have now:
ALTER TRIGGER [dbo].[trg_SourceHistory] ON [dbo].[tblSource]
FOR UPDATE AS
DECLARE @statusOldValue char(1)
DECLARE @statusNewValue char(1)
SELECT @statusOldValue = statusCode FROM deleted
SELECT @statusNewValue= statusCode FROM updated
IF (@statusOldValue <> @statusNewValue) AND
(@statusOldValue = 'P' or @statusOldValue = 'A')
BEGIN TRY
INSERT * INTO tblHistoryTable)
select * from [DELETED]
so I want the new data to stay in the main table, the the history table to be updated with what is being overwritten... right now it just copies the same info over. so after update, both my tables have the same data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只有
Inserted
和Deleted
伪表 - 没有Updated
。对于
UPDATE
,Inserted
包含新值(更新后),而Deleted
包含更新前的旧值。另请注意,触发器每批触发一次 - 而不是每行触发一次。因此,两个伪表都可能包含多行!不要只假设一行并将其分配给一个变量 -
如果您有多行,这将会失败!您需要以这样的方式编写触发器,使其能够在
Inserted
和Deleted
中处理多行!更新:是的 - IS有一种更好的写法:
基本上:
明确指定要插入的列 - 都在
INSERT< /code> 语句以及检索要插入的数据的
SELECT
语句 - 以避免任何令人讨厌的意外在
Inserted
和Deleted
之间创建一个INNER JOIN
code> 伪表来获取所有已更新的行在
SELECT
的WHERE
子句中指定所有其他条件(不同的状态代码等)此解决方案适用于正在更新的批量行 - 它不会在多行更新上失败......
There are only the
Inserted
andDeleted
pseudo tables - there's noUpdated
.For an
UPDATE
,Inserted
contains the new values (after the update) whileDeleted
contains the old values before the update.Also be aware that the triggers is fired once per batch - not once for each row. So both pseudo tables will potentially contain multiple rows! Don't just assume a single row and assign this to a variable - this
will fail if you have multiple rows ! You need to write your triggers in such a fashion that they work with multiple rows in
Inserted
andDeleted
!Update: yes - there IS a much better way to write this:
Basically:
explicitly specify the columns you want to insert - both in the
INSERT
statement as well as theSELECT
statement retrieving the data to insert - to avoid any nasty surprisescreate an
INNER JOIN
betweenInserted
andDeleted
pseudo-tables to get all rows that were updatedspecify all other conditions (different status codes etc.) in the
WHERE
clause of theSELECT
This solution works for batches of rows being updated - it won't fail on a multi-row update....
您需要同时使用
inserted
和deleted
表来检查以下记录:1. 已经存在(检查它不是插入)
2.仍然存在(检查它不是删除)
3.状态字段已更改
您还需要确保以基于集合的方法执行此操作,根据 marc_s 的回答,触发器不是单个记录进程。
You need to use both the
inserted
anddeleted
tables together to check for records that:1. Already existed (to check it's not an insert)
2. Still exists (to check it's not a delete)
3. The Status field changed
You also need to make sure you do that in a set based approach, as per marc_s's answer, triggers are not single record processes.
没有
更新
表,您正在寻找插入
。There is no
updated
table, you are looking forinserted
.