Временные столы - ваш друг здесь.:) Возьмите свой @ItemID и разбейте его на временную таблицу, затем присоедините свой отчет к этой временной таблице.
-- We need some variables for working with the data
DECLARE @Sep Char,
@SepPos Int
SET @Sep = '~'
-- We need a place to store our arguments
CREATE TABLE #Values (Val1 VarChar(3), Val2 VarChar(50))
SELECT @SepPos = CharIndex (@Sep, @ItemID)
WHILE @SepPos > 0 BEGIN
-- Parse the leading argument into the temp table
INSERT INTO #Values (Val1, Val2)
SELECT SubString (@ItemID, 1, 3),
SubString (@ItemID, 4, @SepPos - 4)
-- Remove the leading argument from the argument string
SELECT @ItemID = SubString (@ItemID, @SepPos + 1, Len (@ItemID))
-- Find the next separator
SELECT @SepPos = CharIndex (@Sep, @ItemID)
END
-- On the last loop, it won't have a separator, so we'll end up with
-- one last argument to parse into our temp table
INSERT INTO #Values (Val1, Val2)
SELECT SubString (@ItemID, 1, 3),
SubString (@ItemID, 4, Len (@ItemID) - 3)
-- Now join to our report
SELECT *
FROM Reports rt
JOIN ReportOrigin rg ON rg.ReportId = rt.ReportId
JOIN #Values ON
rt.ColA = #Values.Val1 AND rt.ColB = #Values.Val2