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
 Transact-SQL (2000)
 variables

Author  Topic 

Jason_D
Starting Member

11 Posts

Posted - 2002-07-17 : 11:25:39
I have assigned a tablename to a variable.
When I use the variable in a SELECT statement in place of the actual table name, the query analyzer says "must declare x variable"

Am I allowed to do this or is it just a problem with my syntax?
Do i need to convert any datatype etc?
my variable is of type varchar.

Thanks
Jason

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2002-07-17 : 11:38:14
If you're using SQL 2000 then you can do this..

Post your query and we can tell you if it is correct.

Peace

Rick

Go to Top of Page

skond
Yak Posting Veteran

55 Posts

Posted - 2002-07-17 : 11:42:34
this should work

declare @table_name varchar(128)

set @table_name = 'employee'
exec('select * from ' + @table_name)

Go to Top of Page

Jason_D
Starting Member

11 Posts

Posted - 2002-07-17 : 11:57:28
Here's the section of code where the error is generated:
(the rest is too long, and will probably make things more confusing than helpful)

SELECT x.dbo.tblADM1.Name
FROM @RegionTable INNER JOIN
x.dbo.tblADM1 ON @RegionADM1 = x.dbo.tblADM1.ADM1

---
it's interesting to note that @RegionADM1 does not have the same undeclared problem.
Could the problem arise because I am using a cursor within a cursor?

peace

Go to Top of Page

Jason_D
Starting Member

11 Posts

Posted - 2002-07-17 : 12:06:37
here's a revise version:

SELECT @Province=(SELECT TOP 1 x.dbo.tblADM1.Name
FROM @RegionTable INNER JOIN
x.dbo.tblADM1 ON @RegionADM1 = x.dbo.tblADM1.ADM1
)+' '
FROM etc...
---

can someone revise this for me?
The computer doesn't seem to like @Province=exec('select etc..')
peace

Go to Top of Page
   

- Advertisement -