Use master
GO
Declare @dbName As VARCHAR(50)
SET @dbName = 'TestDB'
CREATE DATABASE @dbName
Above sqlserver is script is giving me error. Why?
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '@dbName'.
If won't work because you're passing a VARCHAR type and for the creation of a database, it takes an object instead of a Varchar. Else, you'll need to pass the whole query as a string to execution;
Declare @dbName As VARCHAR(50) = 'TestDB'
DECLARE @Creation VARCHAR(50) ='CREATE DATABASE '+ @dbName
EXEC(@Creation)
Declare @dbName As VARCHAR(50) Declare @createdbName As VARCHAR(500) SET @dbName = 'TestDB' select @createdbName ='CREATE DATABASE'+ @dbName exec (@createdbName)