Change owner for all tables
Change owner for all tables
Summary: An example of how to change the owner for all tables
- DECLARE @oldOwner sysname, @newOwner sysname, @sql varchar(MAX)
- SELECT @oldOwner = '', @newOwner = 'dbo',
- @sql = '
- IF EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.TABLES
- WHERE
- QUOTENAME(TABLE_SCHEMA)+''.''+QUOTENAME(TABLE_NAME) = ''?''
- AND TABLE_SCHEMA = ''' + @oldOwner + '''
- )
- EXECUTE sp_changeobjectowner ''?'', ''' + @newOwner + ''''
- EXECUTE sp_MSforeachtable @sql