使用Dynamic SQL将文件添加到文件组

时间:2016-04-03 20:34:03

标签: sql cursor dynamic-sql sp-executesql filegroup

希望你能帮助我,我试图添加一个文件,其中包含相应文件组的名称,这是我的代码:

DECLARE @SQL1 nvarchar(1024),
    @SQL2 nvarchar(1024), 
    @name varchar(255);

SET @SQL1 = 'DECLARE vend_cursor CURSOR GLOBAL
           FOR
           SELECT REPLACE(name,''FG'',''FL'') as name FROM sys.filegroups order by name asc';

EXECUTE sp_executesql @SQL1;
OPEN vend_cursor
FETCH NEXT FROM vend_cursor;  

WHILE @@FETCH_STATUS = 0   
BEGIN   

SET  @SQL2 = 'ALTER DATABASE AdventureWorks2012
            ADD FILE 
            (
            NAME='+@name+',
            FILENAME = QuoteName(C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\'+REPLACE(@name,'FL','FG')+'.ndf),
            SIZE = 3MB,
            MAXSIZE = UNLIMITED,
            FILEGROWTH = 10% 
            )TO FILEGROUP FG_AddressType;'
EXECUTE sp_executesql @SQL2;
PRINT REPLACE(@name,'FG','FL')
FETCH NEXT FROM vend_cursor INTO @name;  
END   

CLOSE vend_cursor   
DEALLOCATE vend_cursor

当我执行代码时,我收到以下错误:     Msg 102,Level 15,State 1,Line 5     '('

附近的语法不正确

我的代码有问题吗?提前致谢

0 个答案:

没有答案