Friday, February 17, 2012

CREATE DATABASE script doesnt accept a variable for a FILENAME

I want to create a SP that creates a new database, so I script-ed out the db and paste the script into new SP gui (SQL 2000). I want to pass it a variable for the data/log file location that is not the default location. The original script looks like this:

CREATE DATABASE [PWRR_DDS] ON (NAME = N'PWRR_DDS_Data', FILENAME = N'F:\SQL SERVER FILES\Databases\dbName.mdf' , SIZE = 3118, FILEGROWTH = 10%) LOG ON (NAME = N'PWRR_DDS_Log', FILENAME = N'F:\SQL SERVER FILES\Databases\dbName_Log.LDF' , SIZE = 5000, FILEGROWTH = 10%) COLLATE SQL_Latin1_General_CP1_CI_AS.

I replaced the path of the FILENAME variable like this:


CREATE DATABASE [PWRR_DDS] ON (NAME = N'PWRR_DDS_Data', FILENAME = @.DBPath, SIZE = 5000, FILEGROWTH = 10%) LOG ON (NAME = N'PWRR_DDS_Log', FILENAME = @.LogPath , SIZE = 5000, FILEGROWTH = 10%) COLLATE SQL_Latin1_General_CP1_CI_AS,

declaring the variables as char(500). The error I get is "Incorrect sybtax near'@.DBPath'.

Any ideas for workaround?

Thanks,

EJM

You could build the stmt and EXEC it.

declare @.sqlnvarchar(500)set @.sql ='CREATE DATABASE [' + @.dbname + +'] ON (NAME = ' + @.datafile +', FILENAME = ' + @.DBPath +', SIZE = 5000, FILEGROWTH = 10%) LOG ON (NAME = ' + @.log +', FILENAME = ' + @.LogPath +', SIZE = 5000, FILEGROWTH = 10%) COLLATE SQL_Latin1_General_CP1_CI_AS..'EXEC(@.sql)

No comments:

Post a Comment