declare @i varchar(max); set @i = ( select 'alter table '+TABLE_NAME+' disable trigger all;'+Char(10)+ 'update '+TABLE_NAME+' set repl_modifiedbysite = ' +Convert(varchar,(Select SiteNr from repl_Site where isMainSite=1 and isCurrentSite=1))+Char(10)+ 'where repl_modifiedbysite != (Select SiteNr from repl_Site where isMainSite=1 and isCurrentSite=1)'+';'+Char(10)+ 'alter table '+TABLE_NAME+' enable trigger all;'+Char(10) from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME like 'repl_modifiedbysite' and TABLE_NAME not like 'AUDIT%' and TABLE_NAME not in (select t.TABLE_NAME from INFORMATION_SCHEMA.TABLES t where t.TABLE_TYPE = 'VIEW') order by TABLE_NAME for XML path('')); print @i execute (@i); GO