本文介绍了更改数据库/添加文件/变量文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个文件/文件组添加到现有数据库中,但我需要从变量中获取路径,因为在此脚本完成时它会有所不同.当我在 SQL Management Studio 2008 R2 中检查脚本时,它在 FILENAME = @Path
处返回错误.
I want to add a file/file group to an existing database, but I need to get the path from a variable because it will be different when this script is finalized. When I check the script in SQL Management Studio 2008 R2, it returns an error at FILENAME = @Path
.
如何使用该变量?
不会从命令行运行脚本!
SCRIPT WILL NOT BE RUN FROM THE COMMAND LINE!
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 )
这篇关于更改数据库/添加文件/变量文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!