SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Change the Owner of a Table


Change the Owner of a Table

Author
Message
AndrewSQLDBA
AndrewSQLDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4290 Visits: 3427
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
- Win
- Win
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 707
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"
GilaMonster
GilaMonster
SSC Guru
SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)

Group: General Forum Members
Points: 216389 Visits: 46278
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


GilaMonster
GilaMonster
SSC Guru
SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)

Group: General Forum Members
Points: 216389 Visits: 46278
- 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


AndrewSQLDBA
AndrewSQLDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4290 Visits: 3427
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)

Group: General Forum Members
Points: 216389 Visits: 46278
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search