I'm trying to change the owner of a table:
sp_changeobjectowner 'OWNER.TABLENAME', 'dbo'
But when executing I get the error message:
Msg 15001, Level 16, State 1, Procedure sp_changeobjectowner, Line 62
Object 'OWNER.TABLENAME' does not exist or is not a valid object for this operation.
You must own the table to use ALTER TABLE. To change the schema of a table, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the table's schema.
You can't change the owner of a table. You can create a new table that is owned by NEW_USER , copy the data from the old table to the new table, drop the foreign key constraints that reference the old table, and create new foreign key constraints that reference the new table.
Go to SQL Server Management Studio >> Right Click on the Database >> Go to Properties >> Go to Files and select OWNER.
No, each database can only have one owner.
The correct way to do this in SQL Server 2005 and up is to stop thinking about the prefix as an "owner." The sp_changeobjectowner
procedure has been deprecated since SQL Server 2005, and you should instead be using schema DDL, e.g.:
ALTER SCHEMA dbo TRANSFER [current_owner].tablename;
To check the current "owner" (this may return multiple rows if you have more than one tablename
in multiple schemas):
SELECT s.name
FROM sys.schemas AS s
INNER JOIN sys.tables AS t
ON s.[schema_id] = t.[schema_id]
WHERE t.name = N'tablename';
Also be sure that you spell the object correctly. In a case-sensitive collation, for example, TABLENAME
and tablename
are not the same object, and spelling it with InCorrEcT CaSe could also lead to this error.
Your statement is correct:
EXEC sp_changeobjectowner '<owner>.<tableName>', '<newOwner>'
If the error happend, try to check who is the current owner of the table:
EXEC sp_table_privileges '<tableName>'
To cover the case where a table exists within a constructed schema name like 'Common' (that is not related to a username), then it is the schema owner that needs to be changed.
alter authorization on schema::Common TO dbo;
That will change the objects within the schema to the specified owner 'dbo' and keep the table within 'Common'.
To list schema owners:
select db_name() as Db,
sch.name as SchemaName,
u.Name as Owner
from sys.schemas sch
join sys.sysusers u
on u.uid = sch.principal_id;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With