IN with variable

I have the following query

Select * From Table
Where AssetType  IN(1,2,3)

How would i change it so that there would be a variable for the items being passed in something like this?



DEClare @AssetList as varchar

Select * From Table
Where AssetType  IN( @AssetList)

This will be used in a SSRS report
vbnetcoderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
The only way to pull that off is with dynamic SQL

Declare @AssetList varchar(1000) = '1, 2, 3'

Declare @sql nvarchar(1000) 
SELECT @sql = 'Select * From Table Where AssetType  IN (' +  @AssetList + ') '

exec sp_executesql @sql

Open in new window

0
vbnetcoderAuthor Commented:
Do you know if this will work in a SSRS report?
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Don't know about T-SQL within an SSRS, but it will work in a Stored Procedure.  
Tell me how 1, 2, 3 is determined, as there are other ways to pull off multiple-value parameters in SSRS.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

vbnetcoderAuthor Commented:
they will probable enter them as a comma separated list in SSRS

1, 2, 3
0
vbnetcoderAuthor Commented:
any query that you would suggest with SSRS?
0
Scott PletcherSenior DBACommented:
SSRS has a special "multi_value" parameter that handles this automatically, as long as you've defined the variable as being multi-value to SSRS.

Then you just code:

Select * From Table
 Where AssetType IN (@variable_name)

and SSRS will expand it to the appropriate IN list.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vbnetcoderAuthor Commented:
ty
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.