概述:尝试为 SQL Server 2008 数据库编写触发器。 TableA
和 TableB
具有相同的架构。
目标:在插入 TableA
时,将该行中的所有内容复制到 TableB
中的新行
到目前为止的注释:
使用this question我设法完成了大部分工作,但后来遇到了一个问题
Cannot use text, ntext, or image columns in the 'inserted' and 'deleted' tables.
我只有文本列,但我也想复制它们。
我找到了this website这似乎有一个解决方法,但是,它正在更新,并且我无法将其应用到我的插入示例中...
有什么想法吗?
编辑:目的是向现有产品添加功能,遗憾的是我无法更改 TableA
的架构。
最佳答案
我不确定为什么您不能使用链接到的示例。它应该很简单:
CREATE TRIGGER T_TableA_I
on TableA
after insert
as
set nocount on
insert into TableB (ColumnA,ColumnB,/* Columns in table b */)
select a.ColumnA,a.ColumnB, /* Columns from table a */
from
TableA a
inner join
inserted i
on
a.PKColumn1 = i.PKColumn1 and
a.PKColumn2 = i.PKColumn2 /* Primary Key columns from table A */
当然,您的问题不包含任何表定义,因此上面的内容需要进行大量修改。希望您能够从上面的评论中找出要添加/删除的内容。
关于sql-server - 将触发器复制行插入另一个重复表 SQL Server 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9796696/