Quantcast
Channel: Database Design forum
Viewing all articles
Browse latest Browse all 2729

T-Script Error on resetting Counter

$
0
0
Hello, When I run this T-SQL script that begins a dynamic way to create table partitions, I’m fine until the last step of resetting the Counter back to 0 and get that error. It seems syntactically fine and makes sense and is needed before it goes to the next block of codes, which all also have this same counter reset to 0. I’ve tried redeclaring, omitting it, mod the syntax, but nothing I do works and I always error. What can I try or rewrite with this design? Thanks

‌‌ERROR - Msg 137, Level 15, State 2, Line 27 Must declare the scalar variable "@Counter".
‌‌
USE
DATABASE_BI;
GO
-- sets declares on variables
DECLARE @SQLScript VARCHAR(4096)
DECLARE @MONTHS INT= 84
DECLARE @Counter INT= 0
DECLARE @Path VARCHAR(1024)='G:\'
-- creates filegroups dynamically
WHILE @Counter < @MONTHS
BEGIN
       SET @SQLScript='ALTER DATABASE DATABASE_BI ADD FILEGROUP AA'
       +LTRIM(RTRIM(STR(YEAR(DATEADD(MONTH, @Counter,'2015-01-01')))))
       +RIGHT('0'+LTRIM(RTRIM(STR(MONTH(DATEADD(MONTH, @Counter,'2015-01-01'))))),2)
       +'fg'
       PRINT @SQLScript
       EXEC (@SQLScript)
       SET @Counter= @Counter + 1
END

SET @Counter = 0

‌‌ERROR - Msg 137, Level 15, State 2, Line 27 Must declare the scalar variable "@Counter".
‌‌‌

Viewing all articles
Browse latest Browse all 2729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>