如何在触发器中使用文本列
我试图在 SQL Server 2000 中使用更新触发器,以便在发生更新时,我将一行插入到历史表中,这样我就保留表上的所有历史记录:
CREATE Trigger trUpdate_MyTable ON MyTable
FOR UPDATE
AS
INSERT INTO
[MyTableHistory]
(
[AuditType]
,[MyTable_ID]
,[Inserted]
,[LastUpdated]
,[LastUpdatedBy]
,[Vendor_ID]
,[FromLocation]
,[FromUnit]
,[FromAddress]
,[FromCity]
,[FromProvince]
,[FromContactNumber]
,[Comment])
SELECT
[AuditType] = 'U',
D.*
FROM
deleted D
JOIN
inserted I ON I.[ID] = D.[ID]
GO
当然,我收到错误
不能在“插入”和“删除”表中使用 text、ntext 或 image 列。
我尝试加入 MyTable
而不是删除,但因为插入触发器在插入后触发,所以当我想要原始记录时,它最终会将新记录插入到历史表中。
我怎样才能做到这一点并仍然使用文本列?
I am trying to use an update trigger in SQL Server 2000 so that when an update occurs, I insert a row into a history table, so I retain all history on a table:
CREATE Trigger trUpdate_MyTable ON MyTable
FOR UPDATE
AS
INSERT INTO
[MyTableHistory]
(
[AuditType]
,[MyTable_ID]
,[Inserted]
,[LastUpdated]
,[LastUpdatedBy]
,[Vendor_ID]
,[FromLocation]
,[FromUnit]
,[FromAddress]
,[FromCity]
,[FromProvince]
,[FromContactNumber]
,[Comment])
SELECT
[AuditType] = 'U',
D.*
FROM
deleted D
JOIN
inserted I ON I.[ID] = D.[ID]
GO
Of course, I get an error
Cannot use text, ntext, or image columns in the 'inserted' and 'deleted' tables.
I tried joining to MyTable
instead of deleted, but because the insert trigger fires after the insert, it ends up inserting the new record into the history table, when I want the original record.
How can I do this and still use text columns?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据SQL Server 2000 CREATE TRIGGER
那么,如果我正确地阅读了本文(自从我使用 '2000 以来已经有一段时间了),
According to SQL Server 2000 CREATE TRIGGER
So, if I've read this correctly (and it's been some time since I worked with '2000)
执行此操作的唯一方法是将当前行也保留在历史记录表中。执行您尝试的操作并加入实际表,并将当前实际表中的所有列插入历史表中。
你可能有这样的事情,其中历史记录只有一行的先前版本:
我是说做这样的事情,其中存储每个版本:
这样每当有插入或更新时,只需将当前行插入到在历史表中,您有当前行的重复项,但这并没有那么糟糕。
如果您只是将此触发器添加到其中包含数据的现有表中,则运行如下查询来为您预填充所有当前值:
the only way to do this is keep the current row in the history table too. Do what you tried and join to the actual table, and insert all columns from current actual table into the history table.
you probably have something like this, where the history only has the previous version of a row:
I'm saying do something like this, where every version is stored:
this way whenever there is an insert or update, just insert the current row into the history table, you have a duplicate of the current row, but that isn't that terrible.
If you are just adding this trigger to an existing table with data in it, then run a query like this to prepopulate all the current values for you: