0

I want to execute this query as you can see :

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;

But I get this error in this part +@site_value

Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '+'.

I used CONVERT and CONCAT but I get the same error.

2
  • 1
    you will need to use dynamic sql Commented Aug 2, 2017 at 5:24
  • Try to concat "C:\symfadb2filegroup\data_'+@site_value+'.ndf'," in variable and assign this variable to FileName Commented Aug 2, 2017 at 5:25

1 Answer 1

1

you will need to use dynamic sql like below

 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

total code below

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.