Skip to content

Instantly share code, notes, and snippets.

@BrentOzar
Created June 25, 2016 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrentOzar/5dcebc37adafa2c7142c585e0837820a to your computer and use it in GitHub Desktop.
Save BrentOzar/5dcebc37adafa2c7142c585e0837820a to your computer and use it in GitHub Desktop.
Create a bunch of databases in a loop
DECLARE @StringTemplate VARCHAR(1000);
DECLARE @StringToExecute VARCHAR(1000);
DECLARE @Counter TINYINT = 1;
SET @StringTemplate = 'CREATE DATABASE [ConsumeMassQuantities##REPLACE##]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N''ConsumeMassQuantities'', FILENAME = N''D:\MSSQL\Data\ConsumeMassQuantities##REPLACE##.mdf'' , SIZE = 8192KB , FILEGROWTH = 65536KB )
LOG ON
( NAME = N''ConsumeMassQuantities_log'', FILENAME = N''D:\MSSQL\Data\ConsumeMassQuantities##REPLACE##_log.ldf'' , SIZE = 8192KB , FILEGROWTH = 65536KB )'
WHILE @Counter < 100
BEGIN
SET @StringToExecute = REPLACE(@StringTemplate, '##REPLACE##', @Counter)
EXEC (@StringToExecute);
SET @Counter = @Counter + 1;
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment