我们有 505 个触发器需要设置NOT FOR REPLICATION
标志。找到一个搜索“%AS%”的用户查询并NOT FOR REPLICATION
在此之前添加。
问题是它命中了 AS 的第一个实例,它可能是注释(ie -- Trigger to prevent update of LastUpdated column
) 或触发器名称(ie trgLastUpdated
)。
以下是我们一直在尝试的。有人对找到正确的AS
以及如何放置NOT FOR REPLICATION
在它之前有任何建议吗?
主要查询:
DROP TABLE #TriggersNFR
CREATE TABLE #TriggersNFR (ServerName varchar(250), Dbname varchar(250),TableName varchar(250), TriggerName varchar(250), FixingScript varchar(max))
exec sp_MSforeachdb '
IF ''?'' NOT IN (''tempdb'',''master'',''model'',''msdb'') BEGIN
USE ?
insert into #TriggersNFR
SELECT @@servername,''?'' DBName,OBJECT_NAME(parent_object_id) TableName,
OBJECT_NAME(o.object_id) [Trigger Name],
STUFF (REPLACE(definition,''CREATE TRIGGER'',''ALTER TRIGGER''),
PATINDEX(''%AS%'',REPLACE(definition,''CREATE TRIGGER'',''ALTER TRIGGER'')),0,''
NOT FOR REPLICATION '')
+ ''
GO
''
from ?.sys.objects o
join ?.sys.sql_modules m
on o.object_id = m.object_id
where o.type = ''TR''
and OBJECTPROPERTY(o.object_id, ''ExecIsTriggerNotForRepl'') = 0
AND OBJECTPROPERTY(o.object_id, ''IsMSShipped'') = 0
END
'
select * from #TriggersNFR
order by Dbname
在 FixingScript 列中有示例输出:
-- =============================================
-- Author: #####
-- Create date: 9/14/2012
-- Description: Prevents updating of a L
NOT FOR REPLICATION astUpdated date that would cause app to drop an update when the next one comes along.
-- =============================================
ALTER TRIGGER trgAppUpdates_Upd_LastUpdated
ON APP_REGISTER_UPDATES
AFTER UPDATE
AS
BEGIN
已尝试以不成功的结果更改此设置:
PATINDEX(''%AS%'',
至
PATINDEX(char(13)+''%AS%''
或者
PATINDEX(''%''+char(13)+''AS%''
或者
PATINDEX(''%''+char(13)+char(10)+''AS%''