Click here to Skip to main content
15,670,482 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to select the data from below table where my parameters are  @Subcatid=1,2,32

ID	Subcatid	ContributorID	LocationId
1	1	2	379
2	2	2	379
3	4	3	200
4	32	2	45
5	41	2	379
6	45	2	30
7	4	412	45
8	5	412	379
9	41	408	379
10	45	408	NULL
Dilan Shaminda 12-Aug-14 4:51am    
You have asked the same question here Use IN keyword

This is one way, but may not be the best
DECLARE @Subcatid AS VARCHAR(100) = '2,3,4'
EXEC ('select * from table where Subcatid in ('+ @Subcatid  +')')
Share this answer
Kumarbs 12-Aug-14 5:35am    
Never go for the dynamic queries, unless until it is required. for this small query no need of that.
Amol_B 13-Aug-14 1:02am    
Yes , i Agree... that's what i said 'may not be the best'
You can use IN operator.

Select * from tableName where subcatid in (@Subcatid)
Share this answer
Herman<T>.Instance 12-Aug-14 4:52am    
the @sucatId will holl hold: '2,3,4,...' So it is varchar, not int. You query will not give result.
Kumarbs 12-Aug-14 4:58am    
Well, if the string is constructed at db level, it works perfectly.
@p@richit 12-Aug-14 5:08am    
It works for me :)
create table main (ID int,	Subcatid int,	ContributorID int,	LocationId int)
insert into main values (1,1,2,379),(2,2,2,379),(3,4,3,200),(4,32,2,379)

DECLARE @Subcatid VARCHAR(50)='1,2,32'
DECLARE @myXML AS XML = N'<h><r>' + REPLACE(@Subcatid, ',', '</r><r>') + '</r></h>'

Select * from main where Subcatid In (
SELECT'.', 'INT') AS val
FROM @myXML.nodes('/H/r') AS Vals(id)

drop table main
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900