Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ownership of all objects in a database

Tags:

sql-server

I can change the ownership of a single table using sp_changeobjectowner.

If I want to change ownership of all the objects in a database, should I write a stored procedure to iterate through each object or is there another way?

UPDATE I also found that changing the default schema for my user solved the issue that was causing me to think I needed to change ownership of all the objects.

like image 336
Kirsten Avatar asked May 07 '13 23:05

Kirsten


People also ask

How do I change the owner of a SQL database?

Go to SQL Server Management Studio >> Right Click on the Database >> Go to Properties >> Go to Files and select OWNER. You can see the following screenshot which describes how to do the same task.

How do you change the owner of all table in Postgres?

If you can query the tablenames in your schema, you can generate the queries to ALTER table ownership. For example: select 'ALTER TABLE ' || t. tablename || ' OWNER TO new_owner;' from pg_tables t where t.

How do you change the owner of an object in Oracle?

Answer: The traditional approach for changing a table owner is to use CTAS or export/import. create table bill. emp as select * from scott.

How do you change the owner of a object in SAP?

Right-click the object in the Studio Tree and select Admin Change Owner. The Change Owner dialog appears. Select the new owner from the dropdown list. Save your changes.


1 Answers

Try running this query and then just select all results and execute in separate query

select 'EXEC sp_changeobjectowner ''' + S.name + '.' + O.name + '' + ''', ''new_owner'''
from sys.all_objects O
inner join sys.schemas S
on O.schema_id = S.schema_id
where O.type in ('FN','IF','P','TF','U','V', 'TT', 'TF')
like image 181
Orland Mendes Avatar answered Sep 25 '22 15:09

Orland Mendes