SCS1ST
asked on
Still trying to change owner of objects
I have been working of changing owners of all the objects in a SQL db for two days now. Thanks to some very knowledgeable people on EE I have gotten scripts to do them all at once rather than one at a time. I have managed to change owners of the Tables, Views and Stored Procedures. However I can't get the functions to switch owners. Here's the script that I'm using:
DECLARE @dbObject VARCHAR(255)
DECLARE CurDbObjects CURsoR FOR
SELECT su.NAME + '.' + so.NAME AS OBJECT
FROM sysobjects so , sysusers su
WHERE so.UID = su.UID AND su.NAME <> 'dbo'
AND su.NAME NOT LIKE 'INFORMATION%' AND XTYPE = 'FN'ORDER BY so.NAME
OPEN CurDbObjects
FETCH NEXT FROM CurDbObjects INTO @dbObject
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ('sp_changeobjectowner ''' + @dbObject + ''' , ''dbo''')
FETCH NEXT FROM CurDbObjects INTO @dbObject
END
CLOSE CurDbObjects
DEALLOCATE CurDbObjects
Can anyone tell me why this isn't working and yet did on the other objects. Thanks.
DECLARE @dbObject VARCHAR(255)
DECLARE CurDbObjects CURsoR FOR
SELECT su.NAME + '.' + so.NAME AS OBJECT
FROM sysobjects so , sysusers su
WHERE so.UID = su.UID AND su.NAME <> 'dbo'
AND su.NAME NOT LIKE 'INFORMATION%' AND XTYPE = 'FN'ORDER BY so.NAME
OPEN CurDbObjects
FETCH NEXT FROM CurDbObjects INTO @dbObject
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ('sp_changeobjectowner ''' + @dbObject + ''' , ''dbo''')
FETCH NEXT FROM CurDbObjects INTO @dbObject
END
CLOSE CurDbObjects
DEALLOCATE CurDbObjects
Can anyone tell me why this isn't working and yet did on the other objects. Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Replace with this
AND su.NAME NOT LIKE 'INFORMATION%' AND XTYPE IN ( 'TF', 'FN') ORDER BY so.NAME
You must have a table function in your sysobjects list
AND su.NAME NOT LIKE 'INFORMATION%' AND XTYPE IN ( 'TF', 'FN') ORDER BY so.NAME
You must have a table function in your sysobjects list
ASKER
I get this error when I use that script: (And none of the functions changed owner)
Server: Msg 15505, Level 16, State 1, Procedure sp_changeobjectowner, Line 63
Cannot change owner of object 'elliott._qryStaff_Active_ LoanOffice r' or one of its child objects because the new owner 'dbo' already has an object with the same name.
This the one table that I have not been able to change owner because there is a Function with that exact name and it wouldn't let me rename the function so that I could change the owner of the table to dbo.
Server: Msg 15505, Level 16, State 1, Procedure sp_changeobjectowner, Line 63
Cannot change owner of object 'elliott._qryStaff_Active_
This the one table that I have not been able to change owner because there is a Function with that exact name and it wouldn't let me rename the function so that I could change the owner of the table to dbo.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
aneeshattingal - The last script you gave worked on the functions. Thanks. Now the only object I have left is that one table that has the same name as the function with a dbo owner. I can't rename the function, so how do I chance the owner of the table?
ASKER
This is what I get when using EXEC sp_depends @objname = N'elliott._qryStaff_Active _LoanOffic er':
dbo._qryStaff_Active inline function
dbo._qrystaff_Active_loano fficer inline function
Now what do I do?
dbo._qryStaff_Active inline function
dbo._qrystaff_Active_loano
Now what do I do?
Ok...can you open dbo._qryStaff_Active? Is there an explicit reference to elliott._qryStaff_Active_L oanOfficer ? Do the same for the other query.
ASKER
CREATE FUNCTION dbo._qrystaff_Active_loano fficer()
RETURNS TABLE
AS
RETURN ( SELECT elliott._qryStaff_Active_L oanOfficer .*
FROM elliott._qryStaff_Active_L oanOfficer )
AND
CREATE FUNCTION dbo._qryStaff_Active()
RETURNS TABLE
AS
RETURN ( SELECT elliott._qryStaff_Active_L oanOfficer .*
FROM elliott._qryStaff_Active_L oanOfficer )
I think I'm starting to understand this a little. Both queries reference the table owned by elliot. So couldn't I just delete the two queries, then change the owner of the _qryStaff_Active_LoanOffic er table to dbo, then recreate the queries? Is it that simple?
RETURNS TABLE
AS
RETURN ( SELECT elliott._qryStaff_Active_L
FROM elliott._qryStaff_Active_L
AND
CREATE FUNCTION dbo._qryStaff_Active()
RETURNS TABLE
AS
RETURN ( SELECT elliott._qryStaff_Active_L
FROM elliott._qryStaff_Active_L
I think I'm starting to understand this a little. Both queries reference the table owned by elliot. So couldn't I just delete the two queries, then change the owner of the _qryStaff_Active_LoanOffic
That - or just do this:
ALTER FUNCTION dbo._qrystaff_Active_loano fficer()
RETURNS TABLE
AS
RETURN ( SELECT dbo._qryStaff_Active_LoanO fficer.*
FROM dbo._qryStaff_Active_LoanO fficer )
CREATE FUNCTION dbo._qryStaff_Active()
RETURNS TABLE
AS
RETURN ( SELECT dbo._qryStaff_Active_LoanO fficer.*
FROM dbo._qryStaff_Active_LoanO fficer )
Run these two queries that alter the function to look for dbo instead of elliott. Now you should be able to change the table.
ALTER FUNCTION dbo._qrystaff_Active_loano
RETURNS TABLE
AS
RETURN ( SELECT dbo._qryStaff_Active_LoanO
FROM dbo._qryStaff_Active_LoanO
CREATE FUNCTION dbo._qryStaff_Active()
RETURNS TABLE
AS
RETURN ( SELECT dbo._qryStaff_Active_LoanO
FROM dbo._qryStaff_Active_LoanO
Run these two queries that alter the function to look for dbo instead of elliott. Now you should be able to change the table.
use this to change the owner of the table
sp_changeobjectowner tablename, newownername
you cannot rename a function through query analyzer the only way is to drop and recreate the function.
if you don't want to drop and recreate from query analyzer go to Enterprise manager and rename it there it will automatically drop and recreate the function.
oops i am so late.... sorry ptjcb
imran,
>you cannot rename a function through query analyzer the only way is to drop and recreate the function.
i am not pretty sure, but can't we use sp_rename to rename the function ?
>you cannot rename a function through query analyzer the only way is to drop and recreate the function.
i am not pretty sure, but can't we use sp_rename to rename the function ?
Should be:
ALTER FUNCTION dbo._qryStaff_Active()
RETURNS TABLE
AS
RETURN ( SELECT dbo._qryStaff_Active_LoanO fficer.*
FROM dbo._qryStaff_Active_LoanO fficer )
ALTER FUNCTION dbo._qryStaff_Active()
RETURNS TABLE
AS
RETURN ( SELECT dbo._qryStaff_Active_LoanO
FROM dbo._qryStaff_Active_LoanO
ASKER
ptjcb - That query won't work becasue there isn't a dbo._qryStaff_Active_LoanO fficer. Only an Elliott.._qryStaff_Active_ LoanOffice r and when I try to change owners of that table it won't let me because is says there is already an object named that owned by dbo. Which is the inline function. So I'm thinking my only option is to delete the function. Change owners of the table then recreate the function with a different name.
Yes - drop the function, change owner, create function
ASKER
That worked. Thanks very much for all your help and patience while I learn this.
FN = Scalar function
IF = Inlined table-function
TF = Table function
AND su.NAME NOT LIKE 'INFORMATION%' AND XTYPE IN ('FN','IF','TF')ORDER BY so.NAME
for the functions