2010年12月22日 星期三

SQL Server: Enable table level CDC function

Declare @MyTable Varchar(100)

Declare Mycursor Cursor for
SELECT NAME
from sys.tables
where name in
('123','456','789')
and schema_id = 1
and type = 'U'

Open Mycursor
Fetch next from Mycursor into @MyTable

While @@FETCH_STATUS = 0
Begin
declare @tableName varchar(100)
set @tableName = @MyTable --replace with your Tablename
declare @primaryJoin varchar(max)
set @primaryJoin = ''
declare @SQL nvarchar(max)
set @SQL = ''

Set @SQL = 'exec sys.sp_cdc_enable_table '
Set @SQL = @SQL + ' @source_schema = ''dbo'','
Set @SQL = @SQL + ' @source_name = '+@MyTable+','
Set @SQL = @SQL + ' @role_name = NULL, '
Set @SQL = @SQL + ' @supports_net_changes = 1; '

print @SQL
Fetch next from Mycursor into @MyTable

End

Close MyCursor
Deallocate Mycursor

沒有留言: