Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Change the Owner of a Table Expand / Collapse
Author
Message
Posted Saturday, February 23, 2013 9:05 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, October 14, 2014 9:21 AM
Points: 977, Visits: 3,358
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
Post #1423351
Posted Saturday, February 23, 2013 9:56 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:45 PM
Points: 119, Visits: 490
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"
Post #1423354
Posted Saturday, February 23, 2013 10:33 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:50 AM
Points: 40,209, Visits: 36,618
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 2008, MVP
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

Post #1423364
Posted Saturday, February 23, 2013 10:34 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:50 AM
Points: 40,209, Visits: 36,618
- 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 2008, MVP
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

Post #1423365
Posted Saturday, February 23, 2013 11:06 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, October 14, 2014 9:21 AM
Points: 977, Visits: 3,358
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
Post #1423367
Posted Saturday, February 23, 2013 11:22 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:50 AM
Points: 40,209, Visits: 36,618
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 2008, MVP
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

Post #1423369
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse