レガシ プロセスの完全な回帰テストについて心配する必要がないように、トリガーを使用して Product テーブルの更新を 2 番目のテーブルに記録することを選択します。
トリガーは、更新された列をチェックすることで、実行中のプロセスを判別できます。次に、両方のプロセスによって更新されたすべての製品を返す単純なクエリを作成します。
/* Test tables */
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Product')
DROP TABLE Product
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'ProductChangeLog')
DROP TABLE ProductChangeLog
GO
CREATE TABLE Product (
ProductID INT NOT NULL PRIMARY KEY,
ProductName VARCHAR(50) NOT NULL,
ProductDescription VARCHAR(100) NOT NULL,
ProductPrice MONEY NOT NULL,
ProductAvailQty INT NOT NULL
)
CREATE TABLE ProductChangeLog (
ChangeLogID INT NOT NULL IDENTITY(1,1),
ProductID INT NOT NULL,
ChangeTime DATETIME NOT NULL,
ChangeType VARCHAR(50) NOT NULL,
)
GO
CREATE TRIGGER tr_Product ON Product
FOR UPDATE
AS
INSERT ProductChangeLog (ProductID, ChangeTime, ChangeType)
SELECT
ProductID,
GETDATE(),
-- Log the process which was run depending on columns modified
CASE WHEN UPDATE(ProductName) OR UPDATE(ProductDescription)
THEN 'Process1'
WHEN UPDATE(ProductPrice) OR UPDATE(ProductAvailQty)
THEN 'Process2'
ELSE 'Neither'
END
FROM inserted
GO
INSERT Product
SELECT 1, 'Blue V-Neck', 'Sweater', 25.00, 100 UNION ALL
SELECT 2, 'Green V-Neck', 'Sweater', 30.00, 200 UNION ALL
SELECT 3, 'Black crew', 'T-Shirt', 10.00, 45
/* Change two products in first process */
UPDATE Product
SET ProductDescription = 'Men''s Sweater'
WHERE ProductDescription = 'Sweater'
/* Change three products in second process */
UPDATE Product
SET ProductPrice = ProductPrice + 10
/* See all changes */
SELECT *
FROM ProductChangeLog
DECLARE @today DATETIME
SET @today = DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE()))
/* See all products changed today by both processes */
SELECT *
FROM Product p
WHERE EXISTS (
SELECT *
FROM ProductChangeLog
WHERE ChangeType = 'Process1'
AND ChangeTime > @today
AND ProductID = p.ProductID
)
AND EXISTS (
SELECT *
FROM ProductChangeLog
WHERE ChangeType = 'Process2'
AND ChangeTime > @today
AND ProductID = p.ProductID
)
*SQL Server 2005 でテスト済み