我想执行此查询,如您所见:
DECLARE @site_value INT;
SET @site_value = 1310;
WHILE @site_value <= 1396
BEGIN
ALTER DATABASE AdventureWorksDW
ADD FILE
(NAME = N'data_2002',
FILENAME = N'C:\symfadb2filegroup\data_'+@site_value+'.ndf',
SIZE = 5000MB,
MAXSIZE = 10000MB,
FILEGROWTH = 500MB)
TO FILEGROUP [Filegroup_2002]
SET @site_value = @site_value + 1;
END;
但我在这部分+@site_value
Msg 102,Level 15,State 1,Line 10
“+”附近的语法不正确。
我使用了CONVERT
和CONCAT
,但我得到了同样的错误。
答案 0 :(得分:1)
你需要使用如下的动态sql
set @Sql=' ALTER DATABASE AdventureWorksDW
ADD FILE
(NAME = N''data_2002''
FILENAME = N''C:\symfadb2filegroup\data_'''+cast(@site_value as varchar(4))+'.ndf'',
SIZE = 5000MB,
MAXSIZE = 10000MB,
FILEGROWTH = 500MB)
TO FILEGROUP [Filegroup_2002]'
print @Sql
以下总代码
DECLARE @site_value INT;
SET @site_value = 1310;
declare @Sql nvarchar(max)
WHILE @site_value <= 1396
BEGIN
set @Sql=' ALTER DATABASE AdventureWorksDW
ADD FILE
(NAME = N''data_2002''
FILENAME = N''C:\symfadb2filegroup\data_'''+cast(@site_value as varchar(4))+'.ndf'',
SIZE = 5000MB,
MAXSIZE = 10000MB,
FILEGROWTH = 500MB)
TO FILEGROUP [Filegroup_2002]'
print @Sql
exec(@sql)
SET @site_value = @site_value + 1;
END;