I'm not convinced my title accurately represents my problem, but here is what I have:
DECLARE @ListOfTables TABLE(
RowID INT NOT NULL PRIMARY KEY IDENTITY(1,1)
, TableName VARCHAR(200)
)
DECLARE @RowsToProcess INT
DECLARE @CurrentRow INT
DECLARE @TableName VARCHAR(200)
INSERT INTO @ListOfTables (TableName)
VALUES ('Books')
INSERT INTO @ListOfTables (TableName)
VALUES ('Magazines')
INSERT INTO @ListOfTables (TableName)
VALUES ('Essays')
SET @RowsToProcess = @@ROWCOUNT
SET @CurrentRow = 0
I want to then loop through this table and delete.
WHILE @CurrentRow < @RowsToProcess
BEGIN
SET @CurrentRow = @CurrentRow+1
SELECT @TableName = TableName
FROM @ListOfTables
WHERE RowID = @CurrentRow
DELETE FROM @TableName
END
In actuality, my delete isn't just deleting the table - it has specific criteria. I've simplified it for demonstration purposes.
The error I get here is: Must declare the variable '@TableName'.
I've moved it into the WHILE loop and I receive the same error.
What can I do here? Is it possible to do what I'm trying to do?