SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Must declare the table variable "@tableName".


Must declare the table variable "@tableName".

Author
Message
Dunger
Dunger
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 11
Hi,

Hope someone can offer me some guidance here.

When I try to execute the following tSQL, I get the following error -

Msg 1087, Level 15, State 2, Line 16
Must declare the table variable "@tableName".

==========================
Code
CREATE VIEW swpro.queuesV
AS
SELECT DISTINCT SUBSTRING (swpro.queue_phys_descr, 6, 100) AS queueName
FROM swpro.iql_queues
GO

DECLARE @tableName VARCHAR(100)

-- Create CURSOR and iterate through this list getting required info.
DECLARE cDetail CURSOR FAST_FORWARD READ_ONLY
FOR SELECT queueName
FROM swpro.queuesV

OPEN cDetail

FETCH cDetail INTO @tableName

WHILE @@Fetch_Status = 0
BEGIN
PRINT @tableName
SELECT * FROM @tableName

FETCH cDetail INTO @tableName
END

CLOSE cDetail
DEALLOCATE cDetail

DROP VIEW swpro.queuesV
==========================

I get the feeling I have to declare @tableName as a type TABLE, but is this really required for what I am trying to achieve which is basically use a column value as the table name I want to select from?

*Please Note *
The code I provide here might not be the most efficient or effective to get the result I am after and any suggestions would be appeciated. Still learning tSQL! Smile

Many Thanks in advance for any answers supplied,

Cheers,
Scott
Andras Belokosztolszki
Andras Belokosztolszki
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2233 Visits: 1585
You could use dynamic SQL like:

declare @q varchar(1000)
set @q = 'SELECT * FROM ' + @tableName
exec (@q)

Regards,
Andras



Andras Belokosztolszki, MCPD, PhD
GoldenGate Software
Grant Fritchey
Grant Fritchey
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39368 Visits: 32628
In order to select FROM something, it needs to be a table valued object (table, view, function, temp table, table variable, OPENXML, XQuery). You can run a query against a variable like this:
SELECT @Variable


That should work.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Dunger
Dunger
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 11
Thanks!

Using the Dynamic SQL approach has worked a treat. Smile

Cheers,
Scott
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search