Using CMS to change some, not all, DB owners

  • Is there an easy way to use CMS to change DB owner on a multiple databases/servers at once? What I would like to do is change all databases owned by individuals to be owned by 'sa'.

    Using this query, I can quickly check 200+ servers and find 1000+ databases that are potential problems

    select name, suser_sname(sid) from sys.sysdatabases where suser_sname(sid) like 'domain\%'

    The problem is that we have many generic "system ids" that are in the domain, that can and/or should be the owners, so I cannot just do a global "change all to sa". There are also many database out there owned by local accounts that we want to keep that way.

    Neither sp_changedbowner and ALTER AUTHORIZATION seem equiped for a WHERE clause.

    I tried to do a direct UPDATE sys.sysdatabases SET sid = '0x01' WHERE sid = 'xxx' and got the following error.

    Ad hoc updates to system catalogs are not allowed.

    Any other suggestions to do this with a single keystroke? We've recently cleaned up Jobs and Plans using similar processes, but those tables allowed direct update statements.

  • Forgot to mention, third party tools are not an option and we have nobody in house with any Powershell knowledge.

  • Couldn't you change your Where clause to exclude databases that are owned by the generic accounts that it is okay to have as a database owner? Then ALTER AUTHORIZATION would work, right?.

    I'm not a CMS or PBM expert, but that seems like a possible solution.

  • I'm not a CMS expert either but you could try opening a query window that connects to all of your servers (right click on the group heading and select open query) and run something like the following:

    exec sp_msforeachdb 'IF EXISTS (select name, suser_sname(sid) from sys.sysdatabases where suser_sname(sid) like ''DOMAIN\%'' AND name = ''?'') BEGIN USE ?; sp_changedbowner ''sa'' END'

    sp_msforeachdb if you haven't used it before will execute the string you pass to it for each database on the server. It replaces the ?s with the name of the database. I tried the above doing a PRINT instead of the sp_changedbowner and it seemed to work ok.

    Kenneth

    Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

  • Kenneth,

    I tried

    exec sp_msforeachdb "IF EXISTS (select name, suser_sname(sid) from sys.sysdatabases where suser_sname(sid) IN ('domain\one', 'domain\two')) BEGIN USE ?; sp_changedbowner 'sa' END"

    and got error

    The identifier that starts with 'IF EXISTS (select name, suser_sname(sid) from sys.sysdatabases where suser_sname(sid) in ('domain\one', 'domain\two')) BEGIN USE' is too long. Maximum length is 128.

    When I add all the names I need to the the IN clause it will be much, much longer. Is there a way to override the max length?

  • uk00121 90875 (10/26/2011)


    Kenneth,

    I tried

    exec sp_msforeachdb "IF EXISTS (select name, suser_sname(sid) from sys.sysdatabases where suser_sname(sid) IN ('domain\one', 'domain\two')) BEGIN USE ?; sp_changedbowner 'sa' END"

    and got error

    The identifier that starts with 'IF EXISTS (select name, suser_sname(sid) from sys.sysdatabases where suser_sname(sid) in ('domain\one', 'domain\two')) BEGIN USE' is too long. Maximum length is 128.

    When I add all the names I need to the the IN clause it will be much, much longer. Is there a way to override the max length?

    You could try modifying sp_msforeachdb, but is there a reason why you can't use the LIKE method? suser_sname(sid) LIKE 'us\%'?

    The only other option I can see is to do multiple statements, one user at a time.

    Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply