It requires some SQL magic to get this solved. I used a temporary table and stored procedure to parse two comma separated text entries. The stored procedure is recursive.
Here is the magic:
-- tags1: comma separated tags
-- tags2: comma separated tags
create table #tagselection(
tagname varchar(35) collate SQL_Latin1_General_CP1_CS_AS,
selectiontypeid tinyint,
)
go
create procedure #split
@tag varchar(250)
, @selection tinyint
AS
begin
declare @comma int
declare @lhs nvarchar(50)
print @tag
set @tag = LTRIM(RTRIM(@tag))
set @comma = CHARINDEX(',', @tag)
if (@comma = 0)
begin
insert into #tagselection values (@tag, @selection)
end
else
begin
set @lhs = LEFT(@tag, @comma - 1)
insert into #tagselection values (@lhs, @selection)
set @tag = SUBSTRING(@tag, @comma + 1, 250)
exec #split @tag, @selection -- RECURSIVE CALL!
end
end;
go
declare @tags1 nvarchar(250)
declare @tags2 nvarchar(250)
set @tags1 = ##tags1:string?vb6##
set @tags2 = ##tags2:string?d,d2##
exec #split @tags1, 1;
exec #split @tags2, 2;
-- uncomment next line for debug
-- select * from #tagselection;
When this is run you have your tags in the temporary table #tagselection
. I replaced the original where Tags.TagName in ('vb6')
with
where Tags.TagName in (select tagname
from #tagselection
where selectiontypeid = 1 )
and did the same for the other set of tags where selectiontypeid =2.
I forked your original query here where I implemented above solution.