Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:39:40+00:00 2026-05-25T20:39:40+00:00

I changed the table schema from dbo to db_owner using the SQL statement below

  • 0

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 ''?'', ''' + @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:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:39:41+00:00Added an answer on May 25, 2026 at 8:39 pm

    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:

    enter image description here

    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:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I in change the table name using a query statement? I used
I understand that I can change a sql table using the follow sp: EXEC
How to change column order in a table using SQL query in SQL Server
EDIT: Decided tp use the patch utility and generated the SQL from there using
I am working in Sql Server 2008 and there is a change of table
I have the following database schema (names changed to protect the innocent) Code Table
I am attempting to query my table which has the schema: CREATE TABLE [dbo].[FIN_MeetingDisplay-DefaultConfig](
I imported a bunch of tables from an old sql server (2000) to my
Is there a quick and easy way of telling if a table has changed
A fellow developer changed all the values in the userid column of table map

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.