Finding the database owner using TSQL #SQL

As part of a cleanup initiative we have been reviewing all of our databases and their owners and looking for databases that needed their owner changed. We only wanted to touch the databases where the current owner did not equal the owner we wanted to use. I had to stop and think for a minute about how to get the database owner using TSQL. At first I looked in sys.databases… nope. I did see the owner_sid column and that turned on the lightbulb for me and I remembered the suser_sname() command.

If you want to get the database owner out of sys.databases here is the query you would use:

SELECT name, SUSER_SNAME(owner_sid)

FROM   sys.databases

So yeah I almost forgot about suser_sname() cause it’s not really a command that I use on a daily basis… and hopefully this post helps out someone else who might of forgotten about this handy command.

(that and it removes my “SQL Rap” Entry from being the first thing you see when you come to my page :) hehehe)

Enjoy!!

[Post to Twitter]  [Post to Delicious]  [Post to Digg]  [Post to StumbleUpon] 

Finding the database owner using TSQL #SQL

As part of a cleanup initiative we have been reviewing all of our databases and their owners and looking for databases that needed their owner changed. We only wanted to touch the databases where the current owner did not equal the owner we wanted to use. I had to stop and think for a minute about how to get the database owner using TSQL. At first I looked in sys.databases… nope. I did see the owner_sid column and that turned on the lightbulb for me and I remembered the suser_sname() command.

If you want to get the database owner out of sys.databases here is the query you would use:

SELECT name, SUSER_SNAME(owner_sid)

FROM   sys.databases

So yeah I almost forgot about suser_sname() cause it’s not really a command that I use on a daily basis… and hopefully this post helps out someone else who might of forgotten about this handy command.

(that and it removes my “SQL Rap” Entry from being the first thing you see when you come to my page :) hehehe)

Enjoy!!

[Post to Twitter]  [Post to Delicious]  [Post to Digg]  [Post to StumbleUpon]