1

我正在尝试将新文件组和文件添加到现有数据库中。

我的脚本:

DECLARE @AlterDB NVarchar(4000),
        @FGName NVarchar(30) = 'Partition_DW2_',
        @FileLoc NVarchar(50) ='D:\Data\',
        @CurrentYR NVarchar(10) ='2014' ,
        @FullName Nvarchar(max) 
 Set @FullName = @FileLoc+@FGName+@CurrentYR+'.ndf'
        Print @fullName
et @AlterDB = 'ALTER DATABASE Partition_DWJ2
                        ADD FILE 
                        (   NAME =  '+@FGName+@CurrentYR+',FILENAME ='+@FullName +',SIZE = 2048KB ,MAXSIZE = UNLIMITED,FILEGROWTH = 1024KB
                        )TO FILEGROUP '+@FGName+@CurrentYR

            EXECUTE SP_Executesql @AlterDB ,N'@FullName NVarchar(max)',@FullName=@FullName
            Print @ALterDB 

这给出了错误'Incorrect syntax near 'D:'.'

我做错了什么?

4

2 回答 2

4

好吧,假设您实际上是在使用Set @AlterDB而不是et @AlterDB在脚本上,则需要将文件名括在单引号中:

DECLARE @AlterDB NVarchar(4000),
        @FGName NVarchar(30) = 'Partition_DW2_',
        @FileLoc NVarchar(50) ='D:\Data\',
        @CurrentYR NVarchar(10) ='2014' ,
        @FullName Nvarchar(max) 
 Set @FullName = @FileLoc+@FGName+@CurrentYR+'.ndf'
        Print @fullName
 Set @AlterDB = 'ALTER DATABASE Partition_DWJ2
                        ADD FILE 
                        (   NAME =  '+@FGName+@CurrentYR+',FILENAME ='+CHAR(39)+@FullName+CHAR(39)+',SIZE = 2048KB ,MAXSIZE = UNLIMITED,FILEGROWTH = 1024KB
                        )TO FILEGROUP '+@FGName+@CurrentYR

            EXECUTE SP_Executesql @AlterDB ,N'@FullName NVarchar(max)',@FullName=@FullName
            Print @ALterDB 
于 2013-07-30T15:35:32.177 回答
0

尝试添加另一个反斜杠到您的@FileLoc NVarchar(50) = 'D:\\Data\\'

于 2013-07-30T15:23:05.823 回答