Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Trapping errors when working with linked servers24 Hours of PASS, WIT Edition: Register! Learn! Listen to Me! »
    comments

    Fact: every SQL Server database has an "owner". You can check the owner of a database by running this query:

    1. SELECT NAME, SUSER_SNAME(owner_sid)
    2. FROM   sys.databases
    3. WHERE NAME = 'DatabaseName'

    However, there may come a day when you run into this error:

    There was error outputting database level information for ServerName.DatabaseName.
    Property Owner is not available for Database '[DatabaseName]'. This property may not exist for this object, or may not be retrievable due to insufficient access rights.

    When you log into SQL Server Management Studio, right-click the database, and select Properties, you'll see this dialog box:


    Without a database owner, you can't view the database properties. You may run into issues executing sp_helpdb. You may get an error using "EXECUTE AS OWNER". How do you fix this?

    The Old Way: sp_changedbowner

    Normally, I would use sp_changedbowner. Why? It’s familiar. It’s comfortable, like my favorite socks. But there’s a new way, and it’s time for me to learn that. (Also, Microsoft has indicated it will be removed in a future version.)

    The New Way: ALTER AUTHORIZATION

    I had to fumble around a bit to find this command. I am familiar with using ALTER DATABASE SET… to change many database facets. However, in looking through Books Online, I didn’t see a way to use this to change the database owner. I dug a little further, and found ALTER AUTHORIZATION.

    The BOL syntax is:

    ALTER AUTHORIZATION
    ON [ class_type:: ] entity_name
    TO { SCHEMA OWNER | principal_name }

    class_type ::=
    {
    OBJECT | ASSEMBLY | ASYMMETRIC KEY | CERTIFICATE
    | CONTRACT | TYPE | DATABASE | ENDPOINT | FULLTEXT CATALOG
    | FULLTEXT STOPLIST | MESSAGE TYPE | REMOTE SERVICE BINDING
    | ROLE | ROUTE | SCHEMA | SERVICE | SYMMETRIC KEY
    | XML SCHEMA COLLECTION
    }

    So, to use this: My “class_type” is DATABASE, my “entity_name” is the database name, and my “principal name” is my login. My complete statement looks like this:

    1. ALTER AUTHORIZATION ON DATABASE::AdventureWorks TO grrlgeek

    I ran that successfully, and there are no more errors.

    I can check the owner was changed by running my sys.databases query again.

    1. SELECT NAME, SUSER_SNAME(owner_sid)
    2. FROM   sys.databases
    3. WHERE NAME = 'DatabaseName'

    Lesson learned: don't be afraid of new things!

    About the Author

    User bio imageJes Schultz Borland is a Consultant with Brent Ozar PLF, and a Microsoft SQL Server MVP. She holds an AAS - Programmer/Analyst degree, and has worked with SQL Server since 2007, focusing on Reporting Services and day-to-day administration. She is an active member of PASS, Director of Communication for MADPASS, founder of Tech on Tap, and a LessThanDot.com blogger. She's a frequent presenter at user groups, SQL Saturdays, and other community events. She is also an avid runner and chef.
    Social SitingsTwitterLinkedInLTD RSS Feed
    Instapaper

    3 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis Old habits die hard...I am still using sp_who2, DBCC commands everyday for things that I could do with a Dynamic Management View
    02/15/11 @ 09:30
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Very nice, thanks a lot for sharing this info.
    02/16/11 @ 08:22
    Comment from: ergoface [Member]
    ergoface Just wanted to say thank you for this tip. It helped me create a script to mass change the ownership of databases on several servers.
    01/23/12 @ 13:37

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)