Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Help Groups for Snitz Forums 2000 Users
 Help: Database: MS SQL Server
 TSQL - Get last post multiple times
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

Podge
Support Moderator

Ireland
3776 Posts

Posted - 27 January 2007 :  15:33:04  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
use my_database
go

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'.
I've already declared @tablename. Any ideas ?

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.

Edited by - Podge on 27 January 2007 15:33:43

HuwR
Forum Admin

United Kingdom
20595 Posts

Posted - 27 January 2007 :  15:50:00  Show Profile  Visit HuwR's Homepage
can't see anything that looks obvious
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 27 January 2007 :  16:01:02  Show Profile  Send ruirib a Yahoo! Message
You will need to use it only inside an EXEC command, that's dynamic SQL...


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

pdrg
Support Moderator

United Kingdom
2897 Posts

Posted - 27 January 2007 :  17:23:51  Show Profile  Send pdrg a Yahoo! Message
What Rui says...took me a while chewing through the code, but it will certainly throw an error, probably the one you're getting - good luck?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000 Version 3.4.07