什么是动态构建' AND'的好方法。 SQL的运算符功能?

时间:2014-12-10 08:39:18

标签: sql sql-server tsql

目前,我们的应用程序需要查询以返回满足两个所需要求的结果。例如,我们正在寻找一位既是项目经理的业务分析师。

要将列表传递给此存储过程,我将创建一个可在SQL语句中访问的Id表。下面是我的代码,试图构建和执行查询

到目前为止,我一直无法想出一个成功的解决方案来动态构建一个查询字符串,该字符串在传入存储过程的表参数中的Id之间使用AND运算符。

出于测试目的,我在占用表中有硬编码元素。

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

如果有人可以发布解决方案或指出我的方向来解决这个问题,这将是很好的。我一直在努力解决这个问题。

谢谢, 麦克

2 个答案:

答案 0 :(得分:0)

&而不是采用关系方法,我会避免使用动态SQL。

这样的事情:

DECLARE @OccupationAndListTable dbo.IdList_TableType,
        @RowCount INT,
        @CurrentRow INT,
        @OccupationId INT

INSERT INTO @OccupationAndListTable
SELECT 3
INSERT INTO @OccupationAndListTable
SELECT 12

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
INNER JOIN @OccupationAndListTable AS OLT
    ON OLT.Id = PersonOccupation.OccupationId
WHERE Candidate.IsActive = 1
ORDER BY Person.Name 

答案 1 :(得分:0)

不确定为什么要为此查询使用动态SQL - 您不需要!

DECLARE @OccupationAndListTable dbo.IdList_TableType
INSERT INTO @OccupationAndListTable
SELECT 3
UNION
SELECT 12

DECLARE @occupationCount INT
SELECT @occupationCount = COUNT(1) FROM @OccupationAndListTable 

;WITH po AS (
   SELECT 
       PersonOccupation.PersonId,
       COUNT(1) AS OccupationCount
   FROM PersonOccupation
       JOIN @OccupationAndListTable AS olt ON PersonOccupation.OccupationId = olt.Id
   GROUP BY PersonOccupation.PersonId
)
SELECT
    Candidate.PersonId,
    Person.Name
FROM
    Candidate
    INNER JOIN Person on Candidate.PersonId = Person.Id
    LEFT JOIN po ON Person.Id = po.PersonId
WHERE Candidate.IsActive = 1
    AND ISNULL(po.OccupationCount,0) = @occupationCount