Thanks for your help. I've pasted the current SP below, which is fairly rigid in order for the execution to be as efficient as possible (without resorting to dynamic SQL). At the moment this works very well, but I'm now trying to change this so that the @Type, @GroupID and @Criteria can be repeated up to 50 times - with one single result set returned and no duplicate records. Calling the same SP over and over again for each sub-query and appending the resulting data to a temp table, then returning distinct records would be one solution, but creating temp tables seems to send the cpu usage way too high for my liking.
CREATE PROCEDURE [dbo].[spr_ratings]
(
@Type char(1),
@GroupID int,
@Criteria varchar(7500),
@GroupBy char(1)
)
AS
set nocount on
IF @Criteria = '<all>'
BEGIN
IF @GroupBy = '1'
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE dbo.tbl_07_Employee.GroupID = @GroupID
ORDER BY dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
ELSE
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE dbo.tbl_07_Employee.GroupID = @GroupID
ORDER BY dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
GOTO Label1
END
IF @Type = '1'
BEGIN
IF @GroupBy = '1'
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (dbo.tbl_07_Employee.FirstName + ' ' + dbo.tbl_07_Employee.LastName like @Criteria + '%')
ORDER BY dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
ELSE
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (dbo.tbl_07_Employee.FirstName + ' ' + dbo.tbl_07_Employee.LastName like @Criteria + '%')
ORDER BY dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
GOTO Label1
END
IF @Type = '2'
BEGIN
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (dbo.tbl_07_Employee.EmployeeNo = @Criteria)
GOTO Label1
END
IF @Type = '3'
BEGIN
IF @GroupBy = '1'
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (CHARINDEX(CAST(dbo.tbl_04_Reviewer.ReviewerID AS varchar),@Criteria) > 0)
ORDER BY dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
ELSE
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (CHARINDEX(CAST(dbo.tbl_04_Reviewer.ReviewerID AS varchar),@Criteria) > 0)
ORDER BY dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
GOTO Label1
END
IF @Type = '4'
BEGIN
IF @GroupBy = '1'
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (CHARINDEX(', ' + dbo.tbl_05_Location.Channel + '/' + dbo.tbl_05_Location.Department + ',',', ' + @Criteria + ',') > 0)
ORDER BY dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
ELSE
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (CHARINDEX(', ' + dbo.tbl_05_Location.Channel + '/' + dbo.tbl_05_Location.Department + ',',', ' + @Criteria + ',') > 0)
ORDER BY dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
GOTO Label1
END
IF @GroupBy = '1'
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (CHARINDEX(', ' + dbo.tbl_06_Title.Title + ',',', ' + @Criteria + ',') > 0)
ORDER BY dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
ELSE
SELECT dbo.tbl_05_Location.Channel, dbo.tbl_05_Location.Department, dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName, dbo.tbl_07_Employee.EmployeeNo, dbo.tbl_06_Title.Title, dbo.tbl_07_Employee.Gender, dbo.tbl_07_Employee.HireDate, dbo.tbl_07_Employee.Score1, dbo.tbl_07_Employee.Score2, dbo.tbl_07_Employee.Score3, dbo.tbl_07_Employee.Score4, dbo.tbl_07_Employee.Score5, dbo.tbl_07_Employee.Score6, dbo.tbl_07_Employee.Score7, dbo.tbl_07_Employee.RatingDate, dbo.tbl_07_Employee.ReturnedRatingCard
FROM dbo.tbl_04_Reviewer INNER JOIN dbo.tbl_05_Location ON dbo.tbl_04_Reviewer.ReviewerID = dbo.tbl_05_Location.ReviewerID INNER JOIN dbo.tbl_07_Employee ON dbo.tbl_05_Location.LocationID = dbo.tbl_07_Employee.LocationID INNER JOIN dbo.tbl_06_Title ON dbo.tbl_07_Employee.TitleID = dbo.tbl_06_Title.TitleID
WHERE (dbo.tbl_07_Employee.GroupID = @GroupID) AND (CHARINDEX(', ' + dbo.tbl_06_Title.Title + ',',', ' + @Criteria + ',') > 0)
ORDER BY dbo.tbl_04_Reviewer.ReviewerName, dbo.tbl_07_Employee.FirstName, dbo.tbl_07_Employee.LastName
Label1:
set nocount off
GO