Search This Blog

Monday, February 6, 2012

Change DB owner in SQL Server Database

DECLARE @old sysname, @sql varchar(1000)
 
SELECT
 
 @old = 'oldOwner_CHANGE_THIS'
 
 , @sql = '
 
 IF EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.TABLES
 
 WHERE
 
     QUOTENAME(TABLE_SCHEMA)+''.''+QUOTENAME(TABLE_NAME) = ''?''
 
     AND TABLE_SCHEMA = ''' + @old + '''
 
 )
 
 ALTER SCHEMA dbo TRANSFER ?'
 
EXECUTE sp_MSforeachtable @sql

No comments :