如何在触发器中使用文本列

发布于 2024-08-29 19:08:58 字数 876 浏览 3 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是暖光i 2024-09-05 19:08:58

根据SQL Server 2000 CREATE TRIGGER

在删除、插入或更新中
触发器,SQL Server不允许
text、ntext 或 image 列
插入和删除的引用
表如果兼容性级别是
等于 70。text、ntext 和
插入的图像值和
无法访问已删除的表。到
检索新值
INSERT 或 UPDATE 触发器,加入
插入原始表
更新表。 ...

如果兼容性级别为 80 或
更高,SQL Server允许更新
text、ntext 或 image 列
通过 INSTEAD OF 触发器
表或视图。

那么,如果我正确地阅读了本文(自从我使用 '2000 以来已经有一段时间了),

  • 您可以使用 INSTEAD OF 触发器吗?
  • 兼容级别是多少?

According to SQL Server 2000 CREATE TRIGGER

In a DELETE, INSERT, or UPDATE
trigger, SQL Server does not allow
text, ntext, or image column
references in the inserted and deleted
tables if the compatibility level is
equal to 70. The text, ntext, and
image values in the inserted and
deleted tables cannot be accessed. To
retrieve the new value in either an
INSERT or UPDATE trigger, join the
inserted table with the original
update table. ...

If the compatibility level is 80 or
higher, SQL Server allows the update
of text, ntext, or image columns
through the INSTEAD OF trigger on
tables or views.

So, if I've read this correctly (and it's been some time since I worked with '2000)

  • Can you use an INSTEAD OF trigger?
  • What is the compatibility level?
两个我 2024-09-05 19:08:58

执行此操作的唯一方法是将当前行也保留在历史记录表中。执行您尝试的操作并加入实际表,并将当前实际表中的所有列插入历史表中。

你可能有这样的事情,其中​​历史记录只有一行的先前版本:

YourTable
ID  value1   value2
1   AAA      AAAA
2   BBB      BBBB
3   CCC      CCC3

YourTableHostory
HistoryID HistoryDate ID value1  value2
1         4/17/2010   2  CCC     CCCC
2         4/18/2010   2  CCC     CCC1

我是说做这样的事情,其中​​存储每个版本:

YourTable
ID  value1   value2
1   AAA      AAAA
2   BBB      BBBB
3   CCC      CCC3

YourTableHostory
HistoryID HistoryDate ID  value1  value2
1         4/10/2010   1   AAA      AAAA
2         4/10/2010   2   BBB      BBBB
3         4/10/2010   3   CCC      CCCC
4         4/17/2010   2   CCC      CCC1
5         4/18/2010   2   CCC      CCC2
5         4/19/2010   2   CCC      CCC3

这样每当有插入或更新时,只需将当前行插入到在历史表中,您有当前行的重复项,但这并没有那么糟糕。

如果您只是将此触发器添加到其中包含数据的现有表中,则运行如下查询来为您预填充所有当前值:

INSERT INTO YourTableHostory
        (HistoryDate,ID,value1,value2)
    SELECT
        GETDATE(),ID,value1,value2
        FROM YourTable

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:

YourTable
ID  value1   value2
1   AAA      AAAA
2   BBB      BBBB
3   CCC      CCC3

YourTableHostory
HistoryID HistoryDate ID value1  value2
1         4/17/2010   2  CCC     CCCC
2         4/18/2010   2  CCC     CCC1

I'm saying do something like this, where every version is stored:

YourTable
ID  value1   value2
1   AAA      AAAA
2   BBB      BBBB
3   CCC      CCC3

YourTableHostory
HistoryID HistoryDate ID  value1  value2
1         4/10/2010   1   AAA      AAAA
2         4/10/2010   2   BBB      BBBB
3         4/10/2010   3   CCC      CCCC
4         4/17/2010   2   CCC      CCC1
5         4/18/2010   2   CCC      CCC2
5         4/19/2010   2   CCC      CCC3

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:

INSERT INTO YourTableHostory
        (HistoryDate,ID,value1,value2)
    SELECT
        GETDATE(),ID,value1,value2
        FROM YourTable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文