How to create schema dynamically using dynamic SQL

In this short post I’ll show you how to create database schema using dynamic SQL.

It’s easy, just take a look at the following code:

declare @SchemaName varchar(max)

set @SchemaName = ‘YOUR_SCHEMA_NAME’

if not exists (select 0 from sys.schemas where name=@SchemaName)

exec(‘create schema [‘+@SchemaName+‘]’)

go

 

Enjoy!