Create a stored procedure like this (I will assume you have already created the splitter function):
CREATE PROCEDURE dbo.GetTypeDesc
@strTypes VARCHAR(255)
,@strDelimiter CHAR(1)
AS
BEGIN
SELECT DISTINCT
[Type]
,[TypeDesc]
FROM
#TempTable AS tt
CROSS APPLY
dbo.tvfDelimitedSplit8K(@strTypes,@strDelimiter) AS ds
WHERE
tt.[Type] = ds.Item
END
Then call the procedure by passing in your delimited string as the input parameter. Of course, instead of a temp table as used here for demo purposes you would probably have a more complex query against actual tables.
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable
CREATE TABLE #TempTable (
[ID] INT IDENTITY(1,1) NOT NULL,
[Type] CHAR(1) NULL,
[TypeDesc] CHAR(1) NULL,
PRIMARY KEY (ID),
UNIQUE (ID))
INSERT INTO #TempTable
SELECT 'T','A'
UNION
SELECT 'P','B'
UNION
SELECT 'M','C'
UNION
SELECT 'B','D'
UNION
SELECT 'X','X'
UNION
SELECT 'Y','Y'
UNION
SELECT 'Z','Z'
/* Display sample data */
SELECT
[Type]
,[TypeDesc]
FROM
#TempTable
/* Display the filtered data */
EXEC dbo.GetTypeDesc 'T,P,M,B',',' -- comma delimited
--or
EXEC dbo.GetTypeDesc 'T|P|M|B','|' -- pipe delimited
The Splitter Function is a TABLE-VALUED FUNCTION. So the output of that function is a TABLE just like a view or temp table and thus you can use the output to join to any other table. This turns the entire query into a set-based query that doesn't require additional looping once the function has parsed the delimited string into individual rows.
The input string 'T,P,M,B' could be the output from an HTML dropdown, for example, and then the keys represented in the delimited string are used to cross-filter a query to show only the related rows.