I get the following error:
Msg 1087, Level 15, State 2, Line 16
Must declare the table variable "@table_name".
In the following code:
==========================
==========
=========
DECLARE c CURSOR FOR
SELECT table_name FROM vwAgency_tables WHERE aid = @iAid
OPEN c
FETCH next FROM c INTO @table_name
WHILE @@fetch_status = 0
BEGIN
SELECT @iHitCount = COUNT(*) FROM @table_name WHERE TIN LIKE '%'+@sTIN+'%' -- <---------------
FETCH next FROM c INTO @table_name
END
CLOSE c
DEALLOCATE c
==========================
==========
=========
Is there any way to do this without using EXEC on a varchar?
The end result is that id like to keep a running total count of matches from several tables (those tables are only known at runtime)
Start Free Trial