Change the Owner of a Table

  • Good Morning Everyone.

    I have been searching this site all morning. I know that I posted this same question and received a great answer, but I failed to save the code.

    I would like the change the owner of all tables in a database from 'dbo' to 'Test'. I have already created the schema and user. A fine person on here gave me the query to create all the scripts for each table. I have tried running a query, but I missing something in the code that I have written.

    If someone can help me, I will not let this code be lost.

    Thank You in advance for all your help, suggestions and comments

    Andrew SQLDBA

  • Hope it helps...

    --- Executable scripts to change table owner

    SELECT 'EXEC sp_changeobjectowner '''+ SCHEMA_NAME(schema_id) + '.' + OBJECT_NAME(object_Id) + ''', ''dbo'''FROM sys.tables

    Results :

    EXEC sp_changeobjectowner 'dbo.Transactions_Entry', 'dbo'

    EXEC sp_changeobjectowner 'dbo.Transactions_Login', 'dbo'

    EXEC sp_changeobjectowner 'dbo.Transactions_Entry_0222', 'dbo'

    ----------------------------------------------------------------------------------------------------------------------------------------------------

    --- Executable scripts to alter schema

    SELECT 'ALTER SCHEMA [test] TRANSFER ['

    + SCHEMA_NAME(schema_id) + '].[' + OBJECT_NAME(object_Id) + '];'

    FROM sys.tables

    Results:

    ALTER SCHEMA [test] TRANSFER [dbo].[Transactions_Entry];

    ALTER SCHEMA [test] TRANSFER [dbo].[Transactions_Login];

    ALTER SCHEMA [test] TRANSFER [dbo].[Transactions_Entry_0222];

    WHERE SCHEMA_NAME(schema_id) = 'dbo';

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • AndrewSQLDBA (2/23/2013)


    I would like the change the owner of all tables in a database from 'dbo' to 'Test'.

    Do you want to change the owner of the table or do you want to move it to a different schema? Huge difference between the two.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • - Win (2/23/2013)


    Hope it helps...

    --- Executable scripts to change table owner

    SELECT 'EXEC sp_changeobjectowner '''+ SCHEMA_NAME(schema_id) + '.' + OBJECT_NAME(object_Id) + ''', ''dbo'''FROM sys.tables

    This stored procedure only works with the objects available in Microsoft SQL Server 2000. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use ALTER SCHEMA or ALTER AUTHORIZATION instead. sp_changeobjectowner changes both the schema and the owner. To preserve compatibility with earlier versions of SQL Server, this stored procedure will only change object owners when both the current owner and the new owner own schemas that have the same name as their database user names.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thank You Everyone

    I needed a query to select the correct script that included all the table names for a particular database, and change the owner to a different schema. I finally figured out my problem and was able to write the query correctly.

    Thank You everyone

    Andrew SQLDBA

  • AndrewSQLDBA (2/23/2013)


    ... and change the owner to a different schema.

    There's a very big difference between owner and schema, don't confuse them.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply