为了不必担心遗留流程的完整回归测试,我会选择使用触发器将 Product 表上的更新记录到第二个表。
触发器可以通过检查更新的列来确定正在运行的进程。然后我会创建一个简单的查询来返回所有已被两个进程更新的产品。
/* 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 中测试