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
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 HornSQL Server Data DudeCommented:
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

vbnetcoderAuthor Commented:
Do you know if this will work in a SSRS report?
Jim HornSQL Server Data DudeCommented:
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.
5 Ways Acronis Skyrockets Your Data Protection

Risks to data security are risks to business continuity. Businesses need to know what these risks look like – and where they can turn for help.
Check our newest E-Book and learn how you can differentiate your data protection business with advanced cloud solutions Acronis delivers

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

1, 2, 3
vbnetcoderAuthor Commented:
any query that you would suggest with SSRS?
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.

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:
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.