SQL Server - Grant VIEW CHANGE TRACKING on tables

Compatible from SQL Server 2008

Generate script to grant permission quickly in more tables within database. Change Tracking is a lightweight solution that provides an efficient change tracking mechanism for applications. It is configured on table level and database users would require explicit permissions for non db_owner users to be able to read manipulated data. This can be pretty time consuming if you have a lot of database. Use this script to generate T-SQL for you.
 
Only thing you need is to specify user name on line 1 and optionally filter target tables in WHERE condition on line 8.
 
TIP: When executing in SSMS change results output to text using Ctrl+T or using GUI. Then you can just copy-paste the result to new query.
 
Switch result to text in Management Studio

DECLARE @UserName SYSNAME = N'user name here'

SELECT
	'GRANT VIEW CHANGE TRACKING ON [' + s.name + '].[' + t.name + '] TO [' + @UserName + '];' 
FROM sys.change_tracking_tables tr
	INNER JOIN sys.tables t on t.object_id = tr.object_id
	INNER JOIN sys.schemas s on s.schema_id = t.schema_id
WHERE t.name LIKE '%wildcard here%'

Results

Script results screen:

Results screen

Leave a Reply