Search

Thursday, July 28, 2011

To change owner on all tables in a database

This code will change owner on all tables in a databas. Just change "nisse" to the user you want to be the owner on all the objects. Run the code in QA and choose output as text. Copy the outputen to QA and run. You can also rebuild this as a cursor. The last row is so we dont see the tables from microsoft. You can also this in other situations.

Declare @TableOwner as Nvarchar(50)
set @TableOwner ='SA'
SELECT 'EXEC sp_changeobjectowner '+ @TableOwner +'.' + TABLE_NAME + ''', ''dbo'''
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND OBJECTPROPERTY(
OBJECT_ID( QUOTENAME(TABLE_SCHEMA) + '.' +
QUOTENAME(TABLE_NAME)),
'IsMSShipped') = 0

No comments:

Post a Comment