AndyHopper
Programmer
I tried writing a sql script to create my database. Is is possible to use variables in the CREATE DATABASE command such as I have done below? I've been getting some errors and haven't determined if this usage is allowed.
/* define variables for this script */
DECLARE @DBFilePrefix VARCHAR(255),
@DBFileRoot VARCHAR(255),
@DBDataFile VARCHAR(255),
@DBLogFile VARCHAR(255)
@DBName VARCHAR(255)
/* set database file options */
SET @DBFileRoot = 'c:\program files\microsoft sql server\mssql\data\'
SET @DBFilePrefix = 'agentmaster'
SET @DBName ='agentmastersql'
/* generate the file paths */
SET @DBDataFile = (SELECT (@DBFileRoot + @DBFilePrefix + '_dat.mdf') as DBDataFile)
SET @DBLogFile = (SELECT (@DBFileRoot + @DBFilePrefix + '_log.ldf') as DBLogFile)
/* create agentmastersql database */
CREATE DATABASE @DBName
ON
( NAME = 'agentmaster_dat',
FILENAME = (SELECT @DBDataFile as DBDataFile),
SIZE = 10,
MAXSIZE = UNLIMITED ,
FILEGROWTH = 5 )
LOG ON
( NAME = 'agentmaster_log',
FILENAME = (SELECT @DBLogFile as DBLogFile),
SIZE = 5MB,
MAXSIZE = UNLIMITED ,
FILEGROWTH = 5MB )
GO
/* define variables for this script */
DECLARE @DBFilePrefix VARCHAR(255),
@DBFileRoot VARCHAR(255),
@DBDataFile VARCHAR(255),
@DBLogFile VARCHAR(255)
@DBName VARCHAR(255)
/* set database file options */
SET @DBFileRoot = 'c:\program files\microsoft sql server\mssql\data\'
SET @DBFilePrefix = 'agentmaster'
SET @DBName ='agentmastersql'
/* generate the file paths */
SET @DBDataFile = (SELECT (@DBFileRoot + @DBFilePrefix + '_dat.mdf') as DBDataFile)
SET @DBLogFile = (SELECT (@DBFileRoot + @DBFilePrefix + '_log.ldf') as DBLogFile)
/* create agentmastersql database */
CREATE DATABASE @DBName
ON
( NAME = 'agentmaster_dat',
FILENAME = (SELECT @DBDataFile as DBDataFile),
SIZE = 10,
MAXSIZE = UNLIMITED ,
FILEGROWTH = 5 )
LOG ON
( NAME = 'agentmaster_log',
FILENAME = (SELECT @DBLogFile as DBLogFile),
SIZE = 5MB,
MAXSIZE = UNLIMITED ,
FILEGROWTH = 5MB )
GO