247

I want to change schema name of table Employees in Database. In the current table Employees database schema name is dbo I want to change it to exe. How can I do it ?

Example:

FROM

dbo.Employees

TO

exe.Employees

I tried with this query:

ALTER SCHEMA exe TRANSFER dbo.Employees

But this gives me an error:

Cannot alter the schema 'exe', because it does not exist or you do not have permission.

What did I miss?

5

12 Answers 12

372

Create Schema :

IF (NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'exe')) 
BEGIN
    EXEC ('CREATE SCHEMA [exe] AUTHORIZATION [dbo]')
END

ALTER Schema :

ALTER SCHEMA exe 
    TRANSFER dbo.Employees
5
  • 3
    Can you explain what is sys.scemas and EXEC(...) function ? Mar 18, 2013 at 17:55
  • 11
    sys.schemas is a table that contains all the schemas for the database. The Exec('...') just runs a dynamic SQL call, in this case it is needed because a CREATE SCHEMA command must be the first statement in a query batch and executing as dynamic SQL gets you this. Mar 18, 2013 at 18:03
  • 2
    For all Tables , check this and this to do it in single statement, hope helps some one.
    – Shaiju T
    Jul 3, 2016 at 9:45
  • 1
    Using the alter schema seems extremely slow. (I stopped it after 3 minutes for transfer a small table with 300 rows.) Instead, I used select * into exe.Employees from dbo.Employees Jan 21, 2019 at 21:44
  • For empty tables run fast without any problem.
    – Dany
    Jun 21, 2022 at 10:16
58
ALTER SCHEMA NewSchema TRANSFER [OldSchema].[TableName]

I always have to use the brackets when I use the ALTER SCHEMA query in SQL, or I get an error message.

1
  • Adding square-brackets to the table name, table schema, and/or destination schema did not resolve the issue for me.
    – Dai
    Dec 9, 2021 at 6:07
35

Try below

declare @sql varchar(8000), @table varchar(1000), @oldschema varchar(1000), @newschema   varchar(1000)

  set @oldschema = 'dbo'
  set @newschema = 'exe'

 while exists(select * from sys.tables where schema_name(schema_id) = @oldschema)

  begin
      select @table = name from sys.tables 
      where object_id in(select min(object_id) from sys.tables where  schema_name(schema_id)  = @oldschema)

    set @sql = 'alter schema ' + @newschema + ' transfer ' + @oldschema + '.' + @table

   exec(@sql)
 end
2
  • 1
    I had to use set @sql = 'alter schema [' + @newschema + '] transfer [' + @oldschema + '].' + @table to make sure some special words/characters does not cause error.
    – Stoinov
    Nov 15, 2018 at 11:56
  • This piece of sql will transfer all tables with the "dbo" schema to the "exe" schema. I used it to transfer a custom user schema to "dbo" for development. Jun 20, 2022 at 13:40
17
CREATE SCHEMA exe AUTHORIZATION [dbo]
GO

ALTER SCHEMA exe
TRANSFER dbo.Employees
GO
0
7

Through SSMS, I created a new schema by:

  • Clicking the Security folder in the Object Explorer within my server,
  • right clicked Schemas
  • Selected "New Schema..."
  • Named my new schema (exe in your case)
  • Hit OK

I found this post to change the schema, but was also getting the same permissions error when trying to change to the new schema. I have several databases listed in my SSMS, so I just tried specifying the database and it worked:

USE (yourservername)  
ALTER SCHEMA exe TRANSFER dbo.Employees 
5

Your Code is:

FROM
 dbo.Employees
TO
 exe.Employees

I tried with this query.

ALTER SCHEMA exe TRANSFER dbo.Employees

Just write create schema exe and execute it

3

Check out MSDN...

CREATE SCHEMA: http://msdn.microsoft.com/en-us/library/ms189462.aspx

Then

ALTER SCHEMA: http://msdn.microsoft.com/en-us/library/ms173423.aspx

Or you can check it on on SO...

How do I move a table into a schema in T-SQL

1
  • Can you show me example of how to create the schema in my situation ? Mar 18, 2013 at 17:46
3

For SQL2019:

ALTER SCHEMA [SCEMAYOUWANTTOTRANSFERTO] TRANSFER [CURRENTSCHEMA].[TABLENAME];

Example:

ALTER SCHEMA [dbo] TRANSFER [Curated].[MyTable];
2

Make sure you're in the right database context in SSMS. Got the same error as you, but I knew the schema already existed. Didn't realize I was in 'MASTER' context. ALTER worked after I changed context to my database.

2

In case, someone looking for lower version -

For SQL Server 2000:

sp_changeobjectowner @objname = 'dbo.Employess' , @newowner ='exe'

0

Be very very careful renaming objects in sql. You can cause dependencies to fail if you are not fully away with what you are doing. Having said that this works easily(too much so) for renaming things provided you have access proper on the environment:

exec sp_rename 'Nameofobject', 'ReNameofobject'
3
  • 2
    Unfortunately this won't work in this scenario, sp_rename only works for changing the [name] value of an object. You can't change the schema with it. If you tried exec sp_rename 'dbo.Employees', 'exe.Employees' you would get the name [dbo].[exe.Employees] Mar 18, 2013 at 17:24
  • 1
    ALTER SCHEMA (Name of Schema) TRANSFER (schemaName).(ObjectName);
    – djangojazz
    Mar 18, 2013 at 17:53
  • Also note that when you change the schema of a table, any Views that use that table won't get updated. You'll need to manually update the text (the schema names) in these views. (Sigh...) Aug 16, 2017 at 8:41
0

also you can transfer your data from default schema 'dbo' to your schema from wizard by 1-double click on db diagram 2- right click on your certian entity --> select properties 3- on right at identity , change the schema name

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.