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 2008 Forums
 Transact-SQL (2008)
 Set a Variable equal to more than one constant

Author  Topic 

JeniQ
Starting Member

29 Posts

Posted - 2009-09-21 : 16:19:29
Greetings,
I'd like to set a variable equal to more than one constant.

For example, I have a field called RecordStatus. The options for RecordStatus include A, P, I, R, and D. It is possible to logically group these status into the following categories:
Current = A, P
Historical = I, R, D
All = A, P, I, R, D

In a stored proc that I'm writing, I want to easily reference either Current, Historical or All.

So I was trying something like this:

declare @all nvarchar(35)
set @all = ('A' , 'P', 'R', 'I', 'D')

select ResearchID, RecordStatus from Research
where RecordStatus = @all

But that doesn't work. Any suggestions as how I can achieve my goal?

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2009-09-21 : 19:24:55
What you're describing is an IN statement or dealing with comma-separated values (CSVs).

One way to do this is to create table variables and insert the values you want to equate to each variable, and then you could do something like

SELECT ResearchID, RecordStatus
FROM Research
WHERE RecordStatus IN (Select value from @CurrentTableVar)

You also might want to use the search box on this site and search for the term CSV to find other articles on this topic.

--------------------------------------------
Brand yourself at EmeraldCityDomains.com
Go to Top of Page
   

- Advertisement -