1

我有一个存储过程,我将通过合并表'tablea'中的一些表和更新来计算因子。我已经创建了一个触发器,tablea因此当有更新时,这些记录将输入到新表“tablea_new”中。我对触发器的问题是我将一次更新 6 个月的因素,但有些月份的因素可能没有更新,但它们应该输入到新表中。所以触发器让我很困扰。

所以我用一条If语句插入更新记录

我的If声明代码是

@Action A
SET A.Factor=B.Net/B.Cost,A.Net=B.Net,A.LastModified=sysDatetime(),A.LastModifiedBy=suser_name(),A.Path=B.Path
FROM Tablea A
INNER JOIN ##TEMP3 B ON
A.Year=B.Year AND
A.Month=B.Month AND
A.Media=B.Media
 IF @Action='UPDATE'
   BEGIN


   INSERT INTO Tablea_New (ID,Media,Year,Month,Factor,Net,UpdatedDate,UpdatedBy,FilePath)
SELECT ID,Media,Year,Month,Factor,Net,LastModified,LastModifiedBy,FilePath FROM Tablea
WHERE Media='CNN'AND YEAR=@YEAR AND Net >1
END

你们能给我一些建议吗?我可以进行一些修改还是需要使用触发器:(

4

1 回答 1

1

You can make use of the Change Data Capture (CDC) feature in SQL Server 2008 R2. This will help you to automatically store all changed rows, irrespective of the type of change (i.e. whether it is insert, update or delete).

To enable CDC for your database, use the below script

USE your_database_name
GO 
EXEC sys.sp_cdc_enable_db

To enable CDC for your table, use the below script

EXEC SYS.sp_cdc_enable_table 
@source_schema = N'your_schema_name',
@source_name = N'your_table_name',
@role_name = NULL

Once CDC is enabled, a new table is created under schema 'cdc' to store the changed rows, along with metadata about the change. You may directly access this table to get the required data.

于 2013-09-10T11:53:47.770 回答