Here is the code:
declare
@allcounterids varchar(max),
@counteridquery varchar(max);
select
@allcounterids = stuff((
select
'],[' + cast([CounterId] as varchar(32))
from
AllCounters
WHERE DateTime > '2011-08-15' and DateTime < '2011-08-19' and [Type] = 1
for xml path('')), 1, 2, '') + ']';
Select statement
SELECT [Type], [DateTime], Value, AllCounters.CounterId
FROM AllCounters
WHERE CounterId IN @allcounterids
as you can see i have created the varialble ‘@allcounterids’ and populated data in it, my question is can I use this variable in where clause of Select?
No, you can’t have a CSV string and use it with the IN filter (“predicate”). SQL doesn’t work this way without dynamic SQL: which isn’t needed in this case
It can be done in one go, thus
But, saying that that, why not just do this?
Unless your question is incomplete and lacks information…