我有一个Trigger,MyTrigger,我想让它只更改TABLEA中最近插入的行的DESCRIPTION FIELD,[Description]。如图所示,我有它的基础知识,但它目前只扫描TABLEA上每个INSERT的整个表。
TRIGGER如下.............
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER MyTrigger
ON TABLEA
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE TABLEA
SET [DESCRIPTION] = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE([DESCRIPTION], '…' , '...'),'–','-'),'’',''''),'½','1/2'),'•','+'),'”','"'),'“','"'),'‘',''''),'—','-'),'é','e')
WHERE DESCRIPTION LIKE '%…%'
OR DESCRIPTION LIKE '%–%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%½%'
OR DESCRIPTION LIKE '%•%'
OR DESCRIPTION LIKE '%”%'
OR DESCRIPTION LIKE '%“%'
OR DESCRIPTION LIKE '%‘%'
OR DESCRIPTION LIKE '%—%'
OR DESCRIPTION LIKE '%é%'
END
GO
答案 0 :(得分:2)
我建议在INSTEAD
OF触发器中执行此操作,因此实际上只对表执行一个DDL操作(您也可以忽略此处的WHERE
子句并将替换调用应用于inserted
)。
DROP TRIGGER MyTrigger ON TABLEA;
GO
CREATE TRIGGER dbo.MyTrigger
ON TABLEA
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT TABLEA(col1, col2, ..., Description)
SELECT col1, col2, ...,
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE([DESCRIPTION], '…' , '...'),'–','-'),'’','''')
,'½','1/2'),'•','+'),'”','"'),'“','"'),'‘',''''),'—','-'),'é','e')
FROM inserted;
END
GO
答案 1 :(得分:1)
为什么你不使用像INSERTED
和DELETED
这样的表而不是扫描整个表并更新最后一条记录....
答案 2 :(得分:1)
我会使用inserted
表并使触发器INSTEAD OF
不是AFTER
(示例如下)。
SET NOCOUNT ON;
DECLARE @temp table --temp table that can be modified
(
Column1
, Column1
, Column1
, [DESCRIPTION] --this table should mimic your TABLEA schema
)
INSERT INTO @temp
SELECT *
FROM inserted
UPDATE @temp
SET [DESCRIPTION] = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE([DESCRIPTION], '…' , '...'),'–','-'),'’',''''),'½','1/2'),'•','+'),'”','"'),'“','"'),'‘',''''),'—','-'),'é','e')
WHERE DESCRIPTION LIKE '%…%'
OR DESCRIPTION LIKE '%–%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%½%'
OR DESCRIPTION LIKE '%•%'
OR DESCRIPTION LIKE '%”%'
OR DESCRIPTION LIKE '%“%'
OR DESCRIPTION LIKE '%‘%'
OR DESCRIPTION LIKE '%—%'
OR DESCRIPTION LIKE '%é%'
INSERT INTO TABLEA
(
Column1
, Column1
, Column1
, [DESCRIPTION]
)
SELECT Column1
, Column1
, Column1
, [DESCRIPTION]
FROM @temp
END
GO
答案 3 :(得分:0)
请参阅代码中的最后一行 - 使用您的表格的唯一标识符/(主键?)更改标识符。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER MyTrigger
ON TABLEA
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE TABLEA
SET [DESCRIPTION] = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE([DESCRIPTION], '…' , '...'),'–','-'),'’',''''),'½','1/2'),'•','+'),'”','"'),'“','"'),'‘',''''),'—','-'),'é','e')
WHERE (DESCRIPTION LIKE '%…%'
OR DESCRIPTION LIKE '%–%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%’%'
OR DESCRIPTION LIKE '%½%'
OR DESCRIPTION LIKE '%•%'
OR DESCRIPTION LIKE '%”%'
OR DESCRIPTION LIKE '%“%'
OR DESCRIPTION LIKE '%‘%'
OR DESCRIPTION LIKE '%—%'
OR DESCRIPTION LIKE '%é%')
AND EXISTS (SELECT * FROM Inserted i WHERE i.Identifier = TableA.Identifier)
END
GO