有一个具有更新触发器的数据库表。每当更新一列时,都会自动计算其中一列。
我已经调整了触发器,我想再次在所有行上运行它。在 SQL Server Management Studio 中,如果我在表上选择“编辑前 200 行”并编辑其中一行,则更新触发器将起作用。但是当我编写如下查询时:
UPDATE MyTable
SET SomeIrrelevantColumn = 0
触发器不起作用,应该由触发器计算的列保持不变。
如何在所有行上手动运行触发器?
编辑:这是触发器:
USE [MY_DATABASE]
GO
/****** Object: Trigger [dbo].[MY_TABLE_AUER] Script Date: 04/24/2013 00:05:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[MY_TABLE_AUER]
ON [dbo].[MY_TABLE]
AFTER UPDATE
AS
DECLARE @UPD_COLUMN_A int,
@INS_COLUMN_A int,
@UPD_COLUMN_B int,
@UPD_COLUMN_C varchar(255),
@UPD_COLUMN_D varchar(255),
@UPD_COLUMN_E int,
@UPD_COLUMN_F datetime
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT @UPD_COLUMN_A = _Column_A,
@UPD_COLUMN_B =_Column_B,
@UPD_COLUMN_C = COLUMN_C,
@UPD_COLUMN_D = [Column_D]
FROM DELETED;
IF @UPD_COLUMN_D not like '%SomeString%'
BEGIN
SELECT @INS_COLUMN_A = _Column_A
FROM INSERTED;
IF @UPD_COLUMN_A != @INS_COLUMN_A
BEGIN
SELECT @UPD_COLUMN_E = MAX([_Column_B]),
@UPD_COLUMN_F = MAX([_Column_G])
FROM MY_TABLE
WHERE COLUMN_C = @UPD_COLUMN_C
AND [Column_D] LIKE '%SomeString%';
UPDATE MY_TABLE
SET COLUMN_E = @UPD_COLUMN_E,
COLUMN_F = @UPD_COLUMN_F
WHERE [_Column_B] = @UPD_COLUMN_B;
UPDATE MY_TABLE
SET COLUMN_H = @UPD_COLUMN_B
WHERE [_Column_B] = @UPD_COLUMN_E;
END
END
END