Showing posts with label alter table schema. Show all posts
Showing posts with label alter table schema. Show all posts

CREATE new schema and ALTER table schema

To create a new Database Schema:
CREATE SCHEMA NewSchema
GO

To create a new Database Schema only if it does not exist:
IF NOT EXISTS (SELECT * FROM sys.schemas
WHERE name ='NewSchema')

EXEC dbo.sp_executesql @statement=N'
CREATE SCHEMA NewSchema'
;
GO


To change a table's Schema:
ALTER SCHEMA NewSchema TRANSFER OldSchema.TableName;
--default schema is dbo then:
ALTER SCHEMA NewSchema TRANSFER dbo.TableName;