我想将文件/文件组添加到现有数据库中,但是我需要从变量获取路径,因为在此脚本完成后它将有所不同。当我在SQL Management Studio 2008 R2中检查脚本时,它在处返回错误FILENAME = @Path。
FILENAME = @Path
如何使用该变量?
脚本将不会从命令行运行!
ALTER DATABASE [MyDB] ADD FILEGROUP [MyDB_FileStream] CONTAINS FILESTREAM GO DECLARE @Path VARCHAR(MAX) SET @Path = 'C:\whatEverIWantItToBe\ThisCouldChangeWithLogic\YouGetThePoint\' ALTER DATABASE [MyDB] ADD FILE (NAME = 'MyDB_FileStream' , FILENAME = @Path ) TO FILEGROUP [MyDB_FileStream]
使用动态SQL:
Declare @Path nvarchar(max) Declare @Sql nvarchar(max) Set @Path = 'C:\whatEverIWantItToBe\ThisCouldChangeWithLogic\YouGetThePoint\' Set @Sql = 'Alter Database [MyDb] Add File( Name = ''MyDb_FileStream'' , FileName = ' + QuoteName( @Path, '''' ) + ') To FileGroup [MyDbFileStream]' Exec( @Sql )