Here is the query that I am trying to run
DECLARE @str VARCHAR(10)
DECLARE @index int
DECLARE @SQL VARCHAR(100)
DECLARE @SQL2 VARCHAR(300)
DECLARE @SQL3 VARCHAR(400)
SET @str = 'DB'
SET @index = 0
WHILE @index < 100
BEGIN
SET @SQL = 'use ' + @str + CASE
WHEN @index < 10 THEN '00'
WHEN @index < 100 THEN '0'
ELSE ''
END + CAST(@index as VARCHAR)
EXEC(@SQL)
GO
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
set @index = @index + 1
END
which I think should create a table per database in the databases named DB000
to DB099
but I get 3 syntax errors they are posted below
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near ')'.Msg 137, Level 15, State 2, Line 9
Must declare the scalar variable "@index".Msg 137, Level 15, State 2, Line 11
Must declare the scalar variable "@index".
Can someone help me out in making this work correctly? Or at least point me in the right direction?