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)
 using a variable in SET @local_variable

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-04-22 : 22:06:56
Shannon writes "SQL Team,

I'm stumped. I'm also kind of new to this. I did notice an article posted (Dynamic SQL or How do I SELECT TOP @var records?) that looked a little like my problem but I wasn't really able to apply it to what I'm trying to do.

I'm trying to write a pretty simple query:

use wilson
go
declare @fid int, declare @a float, declare @b float, declare @tile float

set @fid = (select fid from (select top 1 (emaxx - eminx)*(emaxy - eminy) env_area, fid from wilson.f5 order by 1 desc) big_area)

set @a = (select (emaxy - eminy) + (emaxx - eminx) from wilson.f5 where fid = @fid)

set @b = (select (emaxy - eminy) * (emaxx - eminx) from wilson.f5 where fid = @fid)

set @tile = (@a + sqrt(square(@a) + 3996*@b)) / 1998

select ceiling(@tile)
go

This query finds the largest derived value from a table and that records associated id, then based on that id, picks values from the same table & plugs them into an equation. What I want to be able to do is update the query to select from a different table without having to change the query in 3 different place (e.g instead of chosing wilson.f5, using wilson.f6). So, I'd like to store the value 'wilson.f5' as a variable. However, when I declare & set the variable I seem unable to use it within the set statements for the @fid, @a & @b variables. I get a 137 error telling me that the variable hasn't been declared. Why am I able to use the @fid variable in the set statement for @a & @b, but not a variable representing the table name?

Thanks for your time,

Shannon"
   

- Advertisement -