473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Query dbowner

Hi

We have a script that allows customers to change the users of the
database including the database owner. This script can be run at any
time. However, sometimes (and it really is only sometimes!) when the
following statement executes:

"exec sp_changedbowner @USER_OWNER"

the following error is reported:

"The proposed new database owner is already a user in the database."

I have checked the other postings and the Books Online but cannot find
a way to query the current name of the dbowner i.e. the value of
@USER_OWNER above. We have to be able to do this within the script so
that if the @USER_OWNER variable is already the dbowner we do not
bother to execute the statement as otherwise this could display the
error. Can anybody help?

Thanks in Advance

Paul

Jul 23 '05 #1
5 9848
EM gets it from exec sp_MSdbuseraccess N'db', N'%'

which calls

select o.name, o.version, o.crdate, suser_sname(o.sid), o.dbid, o.status,
o.category, o.status2, DatabaseProperty(o.name, N'isfulltextenabled')
from master.dbo.sysdatabases o where o.name = @qual

so

select suser_sname(o.sid) from master.dbo.sysdatabases where name = 'DBNAME'
should do it.

Danny

<pa***********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi

We have a script that allows customers to change the users of the
database including the database owner. This script can be run at any
time. However, sometimes (and it really is only sometimes!) when the
following statement executes:

"exec sp_changedbowner @USER_OWNER"

the following error is reported:

"The proposed new database owner is already a user in the database."

I have checked the other postings and the Books Online but cannot find
a way to query the current name of the dbowner i.e. the value of
@USER_OWNER above. We have to be able to do this within the script so
that if the @USER_OWNER variable is already the dbowner we do not
bother to execute the statement as otherwise this could display the
error. Can anybody help?

Thanks in Advance

Paul

Jul 23 '05 #2
This works fine (except missing o for alias for
master.dbo.sysdatabases) and is just what I needed.

Thanks Danny.

Jul 23 '05 #3
This works fine (except missing o for alias for
master.dbo.sysdatabases) and is just what I needed.

Thanks Danny.

Jul 23 '05 #4
One method to retrieve the current database owner:

DECLARE @CurrentOwner nvarchar(256)
SELECT @CurrentOwner = SUSER_SNAME(sid)
FROM master..sysdatabases
WHERE name = DB_NAME()

You can then conditionally change the owner:

IF @CurrentOwner <> @USER_OWNER
EXEC sp_changedbowner @USER_OWNER

However, your error message is most likely because the new database owner is
a current database user rather than the database owner. The query below
will identify this condition as well as the database owner.

SELECT
l.name as LoginName,
u.name AS UserName
FROM master..syslogins l
JOIN sysusers u ON u.sid = l.sid
WHERE l.sid = SUSER_SID(@USER_OWNER)

--
Hope this helps.

Dan Guzman
SQL Server MVP

<pa***********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi

We have a script that allows customers to change the users of the
database including the database owner. This script can be run at any
time. However, sometimes (and it really is only sometimes!) when the
following statement executes:

"exec sp_changedbowner @USER_OWNER"

the following error is reported:

"The proposed new database owner is already a user in the database."

I have checked the other postings and the Books Online but cannot find
a way to query the current name of the dbowner i.e. the value of
@USER_OWNER above. We have to be able to do this within the script so
that if the @USER_OWNER variable is already the dbowner we do not
bother to execute the statement as otherwise this could display the
error. Can anybody help?

Thanks in Advance

Paul

Jul 23 '05 #5
Thanks to both of you for replying to my post.
I have now solved this and proven that both methods will work.

Thanks.

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
14
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. ...
2
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the...
5
by: Ryan Hubbard | last post by:
Is it possible to get the recordset from an open query window? So you run the query. The window is open. Can vba retrieve this data?
4
by: ashwingawande | last post by:
Hi All, I have created a linked server on SQL Server 2005 to access data from DB2. I am able to execute it with the command "SELECT * FROM OPENQUERY(DB2SRV, 'SELECT * FROM dbname.dbowner.tblname')...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.