Using Local Variable as FILENAME in CREATE DATABASE Statement

0 pts.
Tags:
SQL
T-SQL
I am trying to use this statement in SQL Server 2000 :- CREATE DATABASE [Maintenance] ON (NAME = N'Maintenance', FILENAME = @TableName , SIZE = 2, FILEGROWTH = 10%) LOG ON (NAME = N'Maintenance_log', FILENAME = @LogFileName , SIZE = 1, FILEGROWTH = 10%) COLLATE Latin1_General_CI_AS GO The statement works OK if I hardcode the actual FILENAME i.e. N'D:DATAMaintenanceMaintenance.mdf' But the version using local variables throws an error Incorrect syntax near '@TableName'. Is their any way at all in SQL Server 2000 to achieve the use of local variables in this way?
ASKED: May 22, 2007  7:23 AM
UPDATED: May 22, 2007  10:15 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

TheHutt:

You can use dynamic SQL to create the database:

DECLARE @TableName NVARCHAR(1000)
DECLARE @LogFileName NVARCHAR (1000)
DECLARE @SQL NVARCHAR(4000)


SET @SQL =
N’CREATE DATABASE [Maintenance] ON (NAME = N’ + N”” + N’Maintenance’ + N”” +
N’, FILENAME = N’ + N”” + @TableName + N””+ N’, SIZE = 2, FILEGROWTH = 10%)’ +
N’ LOG ON (NAME = N’ + N”” + N’Maintenance_log’ + N”” + N’, FILENAME = N’ + N”” +
@LogFileName + N”” + N’ , SIZE = 1, FILEGROWTH = 10%) COLLATE Latin1_General_CI_AS’

EXEC (@SQL)

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following