CREATE SCHEMA Syntax Error

Bradley Schacht is a Principal Program Manager on the Microsoft Fabric product team based in Saint Augustine, FL. Bradley is a former consultant, trainer, and has coauthored 6 SQL Server and Power BI books, most recently Learn Microsoft Fabric. As a member of the Microsoft Fabric product team, Bradley works directly with customers to solve some of their most complex data problems and helps shape the future of Microsoft Fabric. Bradley gives back to the community through speaking at events such as the SQLBits, Fabric Community Conference, PASS Community Data Summit, SQL Saturdays, Code Camps, and user groups across the country including locally at the Jacksonville SQL Server User Group (JSSUG). He is a contributor on SQLServerCentral.com and blogs on his personal site, BradleySchacht.com.
Recently while creating a script to migrate the new holding tables over to the stage and production machines I ran into a little issue. The holding table we are using in this case houses several different sets of data for our migration project. Tables that are used only for going from the old system to the new (which will be going away in a few months) and tables that will be used in the future while on the new system. In order to keep these tables separated we decided put them in different schemas inside the holding database. For this instance they will be called leaving and future. First we are checking to see if schema already exists, which is not an issue. IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'Current') From there we need to create the schema if it doesn't already exist, also not an issue. A simple piece of code: CREATE SCHEMA Current The entire code would logically be the combination of the two. However, if you run that, an error will occur saying there is "Incorrect syntax near the keyword 'SCHEMA'". The reason for this is that the CREATE SCHEMA command has to be the first command that is given. If you do want to check to see if a schema already exists before trying to create it there is luckily a way around that. Simply replace CREATE SCHEMA Current with EXEC 'CREATE SCHEMA Current' and everything will run without any errors. The final code is listed below: IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'Current') EXEC 'CREATE SCHEMA Current'



