Thursday, September 10, 2009

SQL Change Table Owner

Need a quick way to change the table owner for multiple tables then try this:


DECLARE @old sysname, @new sysname, @sql varchar(1000)

SELECT
@old = 'CHANGE_name_here_ONLY'
, @new = 'dbo'
, @sql = '
IF EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.TABLES
WHERE
QUOTENAME(TABLE_SCHEMA)+''.''+QUOTENAME(TABLE_NAME) = ''?''
AND TABLE_SCHEMA = ''' + @old + '''
)
EXECUTE sp_changeobjectowner ''?'', ''' + @new + ''''

EXECUTE sp_MSforeachtable @sql

No comments:

Post a Comment

Swidget