Currently our application has a need for a query to return a result that meets two desired requirements. For example, we are looking for a Person who is both a Project Manager AND a Business Analyst.
To pass in the list to this stored procedure I am creating a Table of Id's that can be accessed in the SQL statement. Below is my code attempting to build and execute the query
So far I have been unable to come up with a successful solution to dynamically building a query string that uses the AND operator between the Id's in the table parameter passed into the stored procedure.
For testing purposes I have hard coded elements into the occupation table.
BEGIN
DECLARE @SQLQuery NVARCHAR(MAX)
SET @SQLQuery = N'
SELECT DISTINCT
Candidate.PersonId,
Person.Name
FROM
Candidate
INNER JOIN Person on Candidate.PersonId = Person.Id
LEFT OUTER JOIN PersonOccupation on Candidate.PersonId = PersonOccupation.PersonId
LEFT OUTER JOIN Occupation on PersonOccupation.OccupationId = Occupation.Id
'
--SELECTS ACTIVE CANDIDATES
SET @SQLQuery = @SQLQuery + 'WHERE Candidate.IsActive = 1
'
--QUERY FOR OCCUPATIONLIST TABLE
DECLARE @OccupationAndListTable dbo.IdList_TableType,
@RowCount INT,
@CurrentRow INT,
@OccupationId INT
INSERT INTO @OccupationAndListTable
SELECT 3
INSERT INTO @OccupationAndListTable
SELECT 12
IF EXISTS(SELECT * FROM @OccupationAndListTable)
BEGIN
select * From @OccupationAndListTable
set @RowCount = @@ROWCOUNT
set @CurrentRow = 0
CREATE TABLE #TempOccupation (
IdRowCount int IDENTITY(1,1) NOT NULL,
Id int)
INSERT INTO #TempOccupation
SELECT * FROM @OccupationAndListTable
While @CurrentRow < @RowCount
BEGIN
select @OccupationId = Id
from #TempOccupation o
where o.IdRowCount = @CurrentRow
set @CurrentRow = @CurrentRow + 1
PRINT CAST (@CurrentRow as VARCHAR)
PRINT CAST (@OccupationId as VARCHAR)
SET @SQLQuery = @SQLQuery + ' AND PersonOccupation.OccupationId = CAST(@OccupationId as NVARCHAR) '
END
END
--SETS THE ORDER BY TO PERSON'S NAME
SET @SQLQuery = @SQLQuery + '
ORDER BY
Person.Name
'
PRINT @SQLQuery
EXEC sp_executesql @SQLQuery,
N' @OccupationAndListTable dbo.IdList_TableType READONLY',
@OccupationAndListTable;
END
DROP TABLE #TempOccupation
If anyone can post a solution or point me into the direction to a solution to this problem that would be great. I've been struggling with this for too long now.
Thanks, Mike