Question

I created a user in my DB and now I want to give that user a schema. My question is how do I build a query that will add 30 tables to that schema ?

I know how to creat a schema and give select permision to a user on that schema, but I have no idea how to give that schema the tables I want.

FYI : I use sql server 2010 and i want to build a query that does this, not via UI from SQL Server Management Studio

Was it helpful?

Solution

Use ALTER SCHEMA statement to transfer object from one schema to another:

ALTER SCHEMA new_schema TRANSFER old_schema.table

For example, to construct the query to move all tables from Sales schema to new_schema (AdventureWorks2012), use this:

select 'ALTER SCHEMA new_schema TRANSFER Sales.' + o.name + ';'
from sys.objects o
inner join sys.schemas s on o.schema_id = s.schema_id
where s.name = 'Sales'
and o.type = 'U'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top