How to create schema dynamically using dynamic SQL

Author

Soheil Bakhshi

Microsoft Data Platform

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!


Discover more from BI Insight

Subscribe to get the latest posts sent to your email.

BI Insight podcast

Enjoyed this deep dive? Hear more on Spotify.

New episodes on Power BI, Microsoft Fabric and agentic BI. Follow the show so you never miss one.

About the author

Soheil Bakhshi

Microsoft Data Platform

Leave a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.