Changing a table owner

前端 未结 5 1310
名媛妹妹
名媛妹妹 2021-02-04 00:21

I\'m trying to change the owner of a table:

sp_changeobjectowner \'OWNER.TABLENAME\', \'dbo\'

But when executing I get the error message:

5条回答
  •  情深已故
    2021-02-04 01:00

    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.

提交回复
热议问题