Database ownership

  • Good afternoon.  Does anyone have a script that will go through all the databases on a server and print out the name of the database and the owner?  I'm trying to write a script that will go through and update the ownership of all non-dbo databases.  Thanks.

    Chris

  • Should be pretty easy using an undocumented procedure (xp_execresultset).  This was recently in an article on this site..

    http://www.sqlservercentral.com/columnists/jtshyman/usingxp_execresultsettoobtaindatabasesizeinformati.asp

    The database size script could easily be modified to do what you want.

  • You can use following query to get the owner of database.

    select name,suser_sname(sid) from Master..sysdatabases

     

    Thanks,

    SR

     

    Thanks,
    SR

  • Thanks, SR!

  • When possible, avoid querying systables.  The structure isn't guaranteed to stay the same in the future versions of SQL Server.

    Utilize INFORMATION_SCHEMA views when possible.  It doesn't always give you as much as the system tables, but most of the time, it is perfect for what I have needed, and is the MS recommended way to grab data from system tables.

    select  CATALOG_NAME

    from  INFORMATION_SCHEMA.SCHEMATA

    order by CATALOG_NAME

    would give a full list of databases that you can slim down with conditions.

    Cheers!

    -Chris

  • Thanks for the heads up, Chris.

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

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