How to get use text columns in a trigger

拈花ヽ惹草 提交于 2019-11-29 17:24:50

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

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?
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!