Table creation in non dbo schema

Hi,
   I work with Sql server 2016 . While creating a table it is getting created in the DBO schama and I have no option to change . How can I create a table in a different schema?
Sam OZAsked:
Who is Participating?
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl..
-- Create schema new_schema if it does not already exist
IF (NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'new_schema')) 
    EXEC ('CREATE SCHEMA new_schema AUTHORIZATION dbo')

-- Move your_table to schema new_schema
ALTER SCHEMA new_schema TRANSFER dbo.your_table

Open in new window

0
 
Nitin SontakkeDeveloperCommented:
Or while creating table itself, which is I guess what you are asking for...

create table [schema_name].[table_name]
(
  -- column definition goes here...
)

Open in new window

1
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
When creating an object in SQL Server, the object will be created in the default user schema if no explicit schema has been provided.
To create an object in a different database schema you'll need to explicitly say the schema name, as provided above by Nitin. Mind that the user need to have permissions to create objects in the desired schema.
0
 
ZberteocCommented:
You also have to create the schema first.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.