SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Change DB Owner to sa for multiple DB's


Change DB Owner to sa for multiple DB's

Author
Message
Riken Patel
Riken Patel
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 385
Comments posted to this topic are about the item Change DB Owner to sa for multiple DB's
Ernest Libertucci
Ernest Libertucci
Old Hand
Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)

Group: General Forum Members
Points: 336 Visits: 770
use master
go
exec sp_msforeachdb 'USE ?;if not exists (select 1 from sys.databases where owner_sid = 0x01 and name = db_name()) exec sp_changedbowner ''sa'''
Riken Patel
Riken Patel
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 385
sp_changedbowner is an old procedure which may be removed in future release.
sp_msforeachdb is undocumented so would rather not use in a production system, particularly as I have heard of a few problems with it, such as missing databases.
Ernest Libertucci
Ernest Libertucci
Old Hand
Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)

Group: General Forum Members
Points: 336 Visits: 770
Valid points, I was just exploring an alternative. No offense intended!
aleksey donskoy
aleksey donskoy
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 519
Good Script.
Two things:
1. Why do you need to keep the script in the temp table, and not execute it right away?
2. A databases with a spaces in a name would fail this script. Therefore modyfied the following line as:

SELECT 'ALTER AUTHORIZATION ON DATABASE::[' + name + '] TO sa'

Thanks.
Alex Donskoy
SQL Server DBA
Greeneberg Trauriq PA Miami FL
Riken Patel
Riken Patel
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 385
Good spot on the spaces issue.
Not sure how you are thinking of doing it without a temp table, there would be a few ways but they would not be as tidy I don't think.
Also, with this method, you can substitute the query that inserts into the temp table for a different query and run that exec command, such as dropping multiple tables.
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5860 Visits: 5080
Nice script
The following is missing
AND is_read_only = 0

in select

INSERT INTO @LoopExecuteScripts
SELECT 'ALTER AUTHORIZATION ON DATABASE::' + name + ' TO sa'
FROM sys.databases
WHERE owner_sid <> 0x01
AND state_desc = 'ONLINE' --AND is_read_only = 0



and it will work without errors

Regards
IgorMi

Igor Micev,
SQL Server developer at Seavus
My blog: www.igormicev.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search