We have function that creates a table variable that you can join to:
ALTER FUNCTION [dbo].[fn_sqllist_to_table][Fn_sqllist_to_table](@list as varcharAS VARCHAR(8000),
@delim asAS varcharVARCHAR(10))
RETURNS @listTable tableTABLE(
Position intINT,
Value varchar VARCHAR(8000)
)
AS
BEGIN
declare DECLARE @myPos intINT
set SET @myPos = 1
while charindex WHILE Charindex(@delim, @list) > 0
begin BEGIN
insert into INSERT INTO @listTable
(Position,Value)
values VALUES (@myPos, leftLEFT(@list, charindexCharindex(@delim, @list) - 1))
set SET @myPos = @myPos + 1
if charindex IF Charindex(@delim, @list) = lenLen(@list)
insert into INSERT INTO @listTable
(Position, Value)
values VALUES (@myPos, '')
set SET @list = rightRIGHT(@list, lenLen(@list) - charindexCharindex(@delim, @list))
end END
if len IF Len(@list) > 0
insert into INSERT INTO @listTable
(Position, Value)
values VALUES (@myPos, @list)
Return RETURN
END
So:
@Name varchar(8000) = null // parameter for search values
select * from Tags
where Name in (SELECT value From fn_sqllist_to_table(@Name,',')))
order by Count desc