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
ASP.NET by Example
ASP.NET by Example

Find Prices
Read Review
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 11/21/2009 5:51:31 AM