How to add a new schema to sql server 2008?

Sql ServerSql Server-2008

Sql Server Problem Overview


How do you add a new schema to a database? I am creating a new table and would like to select my own schema from the properties list, but I don't know how to create it. I am using SQL Server Management 2008.

Sql Server Solutions


Solution 1 - Sql Server

Use the CREATE SCHEMA syntax or, in SSMS, drill down through Databases -> YourDatabaseName -> Security -> Schemas. Right-click on the Schemas folder and select "New Schema..."

Solution 2 - Sql Server

Here's a trick to easily check if the schema already exists, and then create it, in it's own batch, to avoid the error message of trying to create a schema when it's not the only command in a batch.

IF NOT EXISTS (SELECT schema_name 
    FROM information_schema.schemata 
    WHERE schema_name = 'newSchemaName' )
BEGIN
	EXEC sp_executesql N'CREATE SCHEMA NewSchemaName;';
END

Solution 3 - Sql Server

I use something like this:

if schema_id('newSchema') is null
	exec('create schema newSchema');

The advantage is if you have this code in a long sql-script you can always execute it with the other code, and its short.

Solution 4 - Sql Server

Best way to add schema to your existing table: Right click on the specific table-->Design --> Under the management studio Right sight see the Properties window and select the schema and click it, see the drop down list and select your schema. After the change the schema save it. Then will see it will chage your schema.

Solution 5 - Sql Server

You can try this:

use database
go

declare @temp as int
select @temp = count(1) from sys.schemas where name = 'newSchema'

if @temp = 0 
begin
	exec ('create SCHEMA temporal')
	print 'The schema newSchema was created in database'
end 
else 
print 'The schema newSchema already exists in database'
go

Solution 6 - Sql Server

In SQL Server 2016 SSMS expand 'DATABASNAME' > expand 'SECURITY' > expand 'SCHEMA' ; right click 'SCHEMAS' from the popup left click 'NEW SCHEMAS...' add the name on the window that opens and add an owner i.e dbo click 'OK' button

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchoboView Question on Stackoverflow
Solution 1 - Sql ServerJoe StefanelliView Answer on Stackoverflow
Solution 2 - Sql ServerMarkView Answer on Stackoverflow
Solution 3 - Sql ServerDasMenschView Answer on Stackoverflow
Solution 4 - Sql Servermohammad sirajView Answer on Stackoverflow
Solution 5 - Sql ServerMatias OrtegaView Answer on Stackoverflow
Solution 6 - Sql ServerEmmanuel Arome MutimbaView Answer on Stackoverflow