ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
Sams Teach Yourself C# Web Programming in 21 Days
Sams Teach Yourself C# Web Programming in 21 Days

Find Prices
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software
SQL: Bulk Owner Change
by Steven A Smith
Tables
Page 1 of 2
Jump to:

To change the ownership for a bunch of tables in a database from one owner to another, you can use the following script developed by David Penton (www.davidpenton.com).  Basically, you need to set the @oldOwner and @newOwner values to the values you want to use, and then it will generate the SQL needed for each change owner operation and run them all using the sp_MSforeachteable procedure that is included in SQL Server.

Bulk Table Change Owner:

DECLARE @oldOwner sysname, @newOwner sysname, @sql varchar(1000)

SELECT
@oldOwner = 'aspalliance'
, @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


Copyright © 2002 Steven A Smith
 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 2/9/2010 2:10:30 PM