2015-02-25 6 views

ответ

6

Вы можете использовать следующие T-SQL скрипт для создания другого T-SQL скрипт, который позволит CHANGE TRACKING функцию на всех таблиц с первичными ключами:

вкладка
-- Step #1: Execute below script having [Results to text] option selected (Ctrl + T) 
SET NOCOUNT ON; 
GO  

-- Is CHANGE TRACKING enabled at database level ? 
IF CONVERT(INT, PARSENAME(CONVERT(NVARCHAR(128), SERVERPROPERTY('ProductVersion')), 4)) >= 10 -- 10 = SQL2008 
BEGIN 
    EXEC sp_executesql N'SELECT * FROM sys.change_tracking_databases db WHERE db.database_id = DB_ID(); ' -- sys.change_tracking_databases is available in SQL2008+ 
    IF @@ROWCOUNT = 0 
    BEGIN 
     RAISERROR('CHANGE TRACKING is not enabled at database level.', 16, 1); 
     RETURN; 
    END 
END 

-- It generates the final T-SQL script 
SELECT N'PRINT ''Enable CHANGE_TRACKING on ' + full_table_name + ''';' 
     + N'ALTER TABLE ' + full_table_name + N' ENABLE CHANGE_TRACKING' + CHAR(13) + CHAR(10) 
     + N'GO' 
FROM (
    SELECT QUOTENAME(s.name) + '.' + QUOTENAME(t.name) AS full_table_name, 
      s.name AS schema_name, t.name AS table_name 
    FROM sys.key_constraints x 
    JOIN sys.tables t ON x.parent_object_id = t.object_id 
    JOIN sys.schemas s ON t.schema_id = s.schema_id 
    WHERE x.[type] = 'PK' 
) y 
ORDER BY schema_name, table_name 

Результаты:

PRINT 'Enable CHANGE_TRACKING on [dbo].[Documents]';ALTER TABLE [dbo].[Documents] ENABLE CHANGE_TRACKING 
GO 
PRINT 'Enable CHANGE_TRACKING on [dbo].[Events]';ALTER TABLE [dbo].[Events] ENABLE CHANGE_TRACKING 
GO 
PRINT 'Enable CHANGE_TRACKING on [dbo].[Names]';ALTER TABLE [dbo].[Names] ENABLE CHANGE_TRACKING 
GO 

Шаг 2: выбрать ll текст из вкладки [Результаты], скопировать текст, открыть новое окно запроса (Ctrl + N), вставить и выполнить этот скрипт. Вкладка

Результаты:

Enable CHANGE_TRACKING on [dbo].[Documents] 
Enable CHANGE_TRACKING on [dbo].[Events] 
Enable CHANGE_TRACKING on [dbo].[Names] 
1

Приведенный ниже сценарий позволит отслеживание изменений на всех таблиц базы данных:

-- This script enables change tracking on all tables with primary keys. 
declare @pkTables as table (name nvarchar(1000)); 
insert into @pkTables (name) 
select distinct tc.TABLE_NAME as name 
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc 
join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name 
where tc.CONSTRAINT_TYPE = 'Primary Key' 

-- Keep track of how many tables we are dealing with 
declare @enabled int 
declare @alreadyEnabled int 
set @enabled = 0 
set @alreadyEnabled = 0 
select count(*) as tablesWithPrimaryKeys from @pkTables 

-- For each table name in primary key tables 
declare @tableName nvarchar(1000) 
while exists (select * from @pkTables) 
begin 
    -- Set the current table name 
    select top 1 @tableName = name from @pkTables order by name asc 

    -- Enable change tracking for this table 
    if exists(
    select sys.schemas.name as schemaName, sys.tables.name as tableName from sys.change_tracking_tables 
    join sys.tables on sys.tables.object_id = sys.change_tracking_tables.object_id 
    join sys.schemas on sys.schemas.schema_id = sys.tables.schema_id 
    where sys.tables.name = @tableName 
) 
    begin 
    set @alreadyEnabled = @alreadyEnabled + 1 
    end 
    else 
    begin 
    exec('alter table ' + @tableName + ' enable change_tracking') 
    set @enabled = @enabled + 1 
    end 

    -- Delete the current table name 
    delete @pkTables where name = @tableName 

end 

-- enabled + alreadyEnabled must equal tablesWithPrimaryKeys 
select @enabled as tablesEnabled, @alreadyEnabled as tablesAlreadyEnabled 
+0

Используйте раствор выше, в сочетании с этим сценарием: http://stackoverflow.com/a/28729205/639133 – mozey

 Смежные вопросы

  • Нет связанных вопросов^_^