0

I am trying to add a date stamp when creating a new database in Microsoft SQL Server Management Studio 18. This is the code I am using:

DECLARE @currentDate AS NVARCHAR(10) = CONVERT(NVARCHAR(10), GETDATE(), 120);
DECLARE @databaseName AS NVARCHAR(50) = 'New_Database_Test_' + @currentDate;

PRINT (@databaseName);

CREATE DATABASE @databaseName;

(I am using the print function to make sure it is outputting correctly), whenever I execute the code, I get the following error:

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '@databaseName'.

I can create a new database if I use the following code:

DECLARE @currentDate AS NVARCHAR(10) = CONVERT(NVARCHAR(10), GETDATE(), 120);
DECLARE @databaseName AS NVARCHAR(50) = New_Database_Test_@currentDate;

PRINT (@databaseName)

CREATE DATABASE @databaseName

but it names the database NewDatabase_@currentDate.

Am I missing something when it comes to the CREATE DATABASE function?

I have tried adding brackets a "" around the New_Database_Test, but I get the same error.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

0 Answers0