Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Problems with a SET @variable PLEASE HELP :)

Author  Topic 

etietje
Starting Member

24 Posts

Posted - 2002-12-30 : 15:22:42
Ok, here is my problem. I'm using the sql below and I keep getting an error telling me to declare the @table variable. What is going on, and is there a way to fix this problem? Thank you in advance :)


declare @totalcount as integer
declare @array_value as varchar(100)
declare @table as varchar(100)

set @table = 'cc002307_qa_12'
set @array_value = 'l1q48'

set @totalcount = (select count(@array_value) from @table)




Edited by - etietje on 12/30/2002 15:48:20

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-12-30 : 16:08:56
Stored procs aren't as flexible as you'd like them to be. You cannot declare varchars to substitute for table or column names; you have to use dynamic SQL or the EXEC() function or something similiar.

Search for EXEC or dynamic SQL on this forum for ideas.

When you say

SELECT * FROM @Table

SQL expects @Table to be a table variable, not a varchar. A table variable is an actual table in memory.


Edited by - jsmith8858 on 12/30/2002 16:10:17
Go to Top of Page

etietje
Starting Member

24 Posts

Posted - 2002-12-30 : 17:50:49
Thanks for the suggestion. I actually had to build a select string and then execute that string into a temp table. It may not be the most efficient way to do things, but for this small report, it will work. Thanks again for the help.

Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2002-12-31 : 08:26:49
Hi:

I guess this is what you are trying to do...

declare @totalcount as integer
declare @array_value as varchar(100)
declare @table as varchar(100)

set @table = 'cc002307_qa_12'
set @array_value = 'l1q48'

EXEC ('SELECT ' + @array_value + ' FROM ' + @table)
SET @totalcount = @@ROWCOUNT

HTH

OS



Edited by - mohdowais on 12/31/2002 08:27:27
Go to Top of Page
   

- Advertisement -