开发者

Object does not exist or is not a valid using 'sp_changeobjectowner'

I changed the table schema from dbo to db_owner using the SQL statement below (SQL 2008 R2):

DECLARE @old sysname, @new sysname, @sql varchar(1000)

SELECT
  @old = 'db_owner'
  , @new = 'dbo'
  , @sql = '
  IF EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.TABLES
  WHERE
      QUOTENAME(TABLE_SCHEMA)+''.''+QUOTENAME(TABLE_NAME) = ''?''
      AND TABLE_SCHEMA = ''' + @old + '''
  )
  EXECUTE sp_changeobjectowner ''?'', '''开发者_C百科 + @new + ''''

EXECUTE sp_MSforeachtable @sql

I need to change it back by switch the old and new name parameters, but I am getting an error:

Msg 15001, Level 16, State 1, Procedure sp_changeobjectowner, Line 75 Object '[db_owner].[language_link]' does not exist or is not a valid object for this operation.

That table does exist though and even with that old db_owner. Any way to fix this?

Here is a screenshot on how I could tell it is still owned by db_owner. Only some tables were moved back properly:

Object does not exist or is not a valid using 'sp_changeobjectowner'


Are you sure you should be using sp_changeobjectowner? (Objects don't really have owners anymore as of SQL 2005.) How did you verify that db_owner.language_link exists? Personally I would use ALTER SCHEMA for this and I would also lean toward catalog views (sys.tables) rather than information_schema.tables. Finally, I wouldn't use the undocumented and unsupported sp_MSforeachtable - I have highlighted issues with sp_MSforeachdb that are likely potential issues here because the code is quite similar.

DECLARE
    @old SYSNAME = N'db_owner',
    @new SYSNAME = N'dbo',
    @sql NVARCHAR(MAX) = N'';

SELECT @sql += CHAR(13) + CHAR(10) + 'ALTER SCHEMA ' + @new 
    + ' TRANSFER ' + QUOTENAME(SCHEMA_NAME([schema_id]))
    + '.' + QUOTENAME(name) + ';'
    FROM sys.tables AS t
    WHERE SCHEMA_NAME([schema_id]) = @old
    AND NOT EXISTS (SELECT 1 FROM sys.tables WHERE name = t.name
    AND SCHEMA_NAME([schema_id]) = @new);

PRINT @sql;
--EXEC sp_executesql @sql;

EDIT adding code to find objects that are common to both schemas. And to move the ones already in the new schema to some dummy schema:

CREATE SCHEMA dummy AUTHORIZATION dbo;
GO
DECLARE 
    @old SYSNAME = N'db_owner',
    @new SYSNAME = N'dbo',
    @sql NVARCHAR(MAX) = N'';

SELECT @sql += CHAR(13) + CHAR(10) + 'ALTER SCHEMA dummy TRANSFER '
    + QUOTENAME(@new) + '.' + QUOTENAME(t1.name) + ';'
    FROM sys.tables AS t1
    INNER JOIN sys.tables AS t2
    ON t1.name = t2.name
    WHERE t1.schema_id = SCHEMA_ID(@new)
    AND t2.schema_id = SCHEMA_ID(@old);

PRINT @sql;
-- EXEC sp_executesql @sql;

But really it just sounds like you messed something up and it requires some manual cleanup...

EDIT adding evidence because OP seems convinced that this code is not working because it is not possible to move things into the dbo schema. No, that is not the case, it's just not possible to move dummy.floob -> dbo.floob if there's already an object named dbo.floob. Note that it may not be a table!

CREATE DATABASE schema_test;
GO
USE schema_test;
GO
CREATE SCHEMA floob AUTHORIZATION dbo;
GO
CREATE TABLE dbo.x(a INT);
CREATE TABLE dbo.y(a INT);
GO

Move all tables from dbo -> floob:

DECLARE
    @old SYSNAME = N'dbo',
    @new SYSNAME = N'floob',
    @sql NVARCHAR(MAX) = N'';

SELECT @sql += CHAR(13) + CHAR(10) + 'ALTER SCHEMA ' + @new 
    + ' TRANSFER ' + QUOTENAME(SCHEMA_NAME([schema_id]))
    + '.' + QUOTENAME(name) + ';'
    FROM sys.tables AS t
    WHERE SCHEMA_NAME([schema_id]) = @old
    AND NOT EXISTS (SELECT 1 FROM sys.tables WHERE name = t.name
    AND SCHEMA_NAME([schema_id]) = @new);

EXEC sp_executesql @sql;
GO
SELECT SCHEMA_NAME([schema_id]),name FROM sys.tables;

Results:

Object does not exist or is not a valid using 'sp_changeobjectowner'

Move all tables back from floob -> dbo:

DECLARE
    @old SYSNAME = N'floob',
    @new SYSNAME = N'dbo',
    @sql NVARCHAR(MAX) = N'';

SELECT @sql += CHAR(13) + CHAR(10) + 'ALTER SCHEMA ' + @new 
    + ' TRANSFER ' + QUOTENAME(SCHEMA_NAME([schema_id]))
    + '.' + QUOTENAME(name) + ';'
    FROM sys.tables AS t
    WHERE SCHEMA_NAME([schema_id]) = @old
    AND NOT EXISTS (SELECT 1 FROM sys.tables WHERE name = t.name
    AND SCHEMA_NAME([schema_id]) = @new);

EXEC sp_executesql @sql;
GO
SELECT SCHEMA_NAME([schema_id]),name FROM sys.tables;

Results:

Object does not exist or is not a valid using 'sp_changeobjectowner'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜