0

I used an existing database to generate a "CREATE"-Script via the MS SQL Server Management Tool. Then i replaced the string literals for the DB Name with variables. However, when i execute the script, it keeps saying "wrong syntax near @DBFullName" (the first use in 'NAME = @DBFullName ...'). I have no idea what the issue is, other than the possibility, that the use of variables is forbidden here.

DECLARE @DBNAME nvarchar(MAX);
SET @DBNAME = 'MyDataBase'

DECLARE @DBFullName text;
SET @DBFullName = 'MySuperDataBase';

DECLARE @DBFileName text;
SET @DBFileName = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DEVELOPMENT\MSSQL\DATA\\' + @DBName + '.mdf';
DECLARE @DBLogName text;
SET @DBLogName = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DEVELOPMENT\MSSQL\DATA\\' + @DBName + '.ldf';

CREATE DATABASE [@DBNAME] ON  PRIMARY 
( NAME = @DBFullName + '_Data', FILENAME = @DBFileName , SIZE = 30720KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
 LOG ON 
( NAME = @DBFullName + '_Log', FILENAME = @DBLogName , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
2
  • Please see the following for a method on doing this stackoverflow.com/questions/727788/… Commented Mar 16, 2015 at 9:05
  • You'll have to use Dynamic SQL for that. Furthermore, I believe text isn't allowed as a variable data type. Commented Mar 16, 2015 at 9:05

1 Answer 1

2

You have to use dynamic SQL. Also, you need to replace text with nvarchar(max) otherwise text concatenation won't work.

Here is full script for you:

DECLARE @DBNAME sysname;
SET @DBNAME = 'MyDataBase'

DECLARE @DBFullName sysname;
SET @DBFullName = 'MySuperDataBase';

DECLARE @DBFileName nvarchar(max);
SET @DBFileName = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DEVELOPMENT\MSSQL\DATA\' + @DBName + '.mdf';
DECLARE @DBLogName nvarchar(max);
SET @DBLogName = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DEVELOPMENT\MSSQL\DATA\' + @DBName + '.ldf';

DECLARE @SQL nvarchar(max);
SET @SQL = N'
  CREATE DATABASE ' + QUOTENAME(@DBNAME) + ' ON PRIMARY
    (NAME = ' + QUOTENAME(@DBFullName + '_Data') + ',
    FILENAME = ' + QUOTENAME(@DBFileName, '''') + ',
    SIZE = 30720KB,
    MAXSIZE = UNLIMITED,
    FILEGROWTH = 10%)
   LOG ON (
     NAME = ' + QUOTENAME(@DBFullName + '_Log') + ',
     FILENAME = ' + QUOTENAME(@DBLogName, '''') + ',
     SIZE = 2048KB,
     MAXSIZE = UNLIMITED,
     FILEGROWTH = 10%)'

PRINT @SQL

EXEC (@SQL)
GO

Works on my machine! :)

Sign up to request clarification or add additional context in comments.

1 Comment

Ah well :\ this means i'm probably better of with implementing the creation in c#, since my script is a bit longer than only the "Create Database"-statement. It's a giant script, generated by the SQL Management Tool, and i only wanted to add the Name of the DB to control that bit throughout the whole script. If i have to do it with string concatenation on every usage of the DB name, there is little to no improvement ;-) But thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.