DECLARE @userCount int DECLARE @tablename sysname DECLARE tnames_cursor CURSOR
FOR SELECT top 4 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND right(TABLE_NAME, 6) = '_FORUM' OPEN tnames_cursor --DECLARE @tablename sysname FETCH NEXT FROM tnames_cursor INTO @tablename
WHILE (@@FETCH_STATUS <> -1) BEGIN IF (@@FETCH_STATUS <> -2) BEGIN
--********************************** -- Declare the variables to store the values returned by FETCH. DECLARE @lastpost varchar(20) DECLARE forum_cursor CURSOR FOR
SELECT TOP 1 F_LAST_POST FROM @tablename WHERE F_LAST_POST > ' ' ORDER BY F_LAST_POST DESC
OPEN forum_cursor
-- Perform the first fetch and store the values in variables. -- Note: The variables are in the same order as the columns -- in the SELECT statement.
FETCH NEXT FROM forum_cursor INTO @lastpost
-- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN -- Concatenate and display the current values in the variables. PRINT 'Last Post: ' + @lastpost
-- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM forum_cursor INTO @lastpost END
CLOSE forum_cursor DEALLOCATE forum_cursor --********************************** --SELECT @tablename = RTRIM(@tablename) --EXEC ('SELECT U_COUNT from ' + @tablename + '') END FETCH NEXT FROM tnames_cursor INTO @tablename END --print @userCount CLOSE tnames_cursor DEALLOCATE tnames_cursor
The line in red gives this error
Server: Msg 137, Level 15, State 2, Line 22
Must declare the variable '@tablename'.