[thelist] SELECT @Variable FROM Table

Warden, Matt mwarden at mattwarden.com
Fri Jul 27 10:06:51 CDT 2001


On Jul 27, Darren had something to say about [thelist] SELECT @Variable...

>I'm using SQL Server 2K and *trying* to select a value from a table where
>the column name is held in a variable.  Is there any way to do this??
>
>For example, if you have a table like:
>
>   create table #Test (
>      ind       integer   identity,
>      jan       integer,
>      feb       integer,
>      mar       integer,
>      ...
>      nov       integer,
>      dec       integer
>   )

Seems like it's your table structure that's making things hard. How about:

create table #Test (
	ind 		integer 	identity,
	month		char(3),
	monthnum	integer
)

Then you've got:

SELECT TOP 1 monthnum
FROM #Test 
WHERE month=@MonthName 
ORDER BY ind ASC

Make sure that @MonthName is eaither declared as an input variable or
DECARE'd as a local variable in the stored procedure.



HTH,


--
mattwarden
mattwarden.com





More information about the thelist mailing list