我想创建一个带有SQL脚本文件的数据库,但是我不能给出参数而不是数据库名称我该如何解决?
Declare @Test as nvarchar(max)
CREATE DATABASE [@Test] ON PRIMARY
( NAME = N'@Test',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\@Test.mdf',
SIZE = 11264KB , MAXSIZE = 51200KB , FILEGROWTH = 10%)
LOG ON
( NAME = N'@Test_log',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\@Test_1.ldf' ,
SIZE = 9216KB , MAXSIZE = 25600KB , FILEGROWTH = 1024KB )
GO
ALTER DATABASE [@Test] SET COMPATIBILITY_LEVEL = 100
GO
答案 0 :(得分:2)
如果您使用SQLCMD compatibility mode in SQL Server Management Studio,则可以使用SQLCMD参数:
:setvar dbname YourDatabase
CREATE DATABASE $(dbname) ON PRIMARY
( NAME = $(dbname),
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\$(dbname).mdf',
SIZE = 11264KB , MAXSIZE = 51200KB , FILEGROWTH = 10%)
LOG ON
( NAME = N'$(dbname)_log',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\$(dbname)_log.ldf' ,
SIZE = 9216KB , MAXSIZE = 25600KB , FILEGROWTH = 1024KB )
GO
ALTER DATABASE $(dbname) SET COMPATIBILITY_LEVEL = 100
GO
也可以使用命令行上的SQLCMD
命令行工具
您可以在Tools > Options
下的SQL Server Management Studio中启用“SQLCMD模式”
此更改仅会影响正在打开的SSMS中的新窗口 - 它不适用于已经打开的窗口。