TSQL如果存在于子查询中

时间:2014-07-09 19:18:43

标签: sql sql-server tsql stored-procedures

我有一个按预期工作的查询但是我希望看到另一个表中存在submissionID。这是我当前的查询。

IF (@action = 'filter')
            BEGIN

                --Get the current active voting session
                DECLARE @session INT
                SET @session = (SELECT sessionID 
                FROM empowermentSessions 
                WHERE status = '1')

                SELECT A.[submissionID],
                       A.[subEmpID],
                       A.[nomineeEmpID],
                       CONVERT (VARCHAR (10), A.[submissionDate], 101) AS submissionDate,
                       A.[situation],
                       A.[task],
                       A.[action],
                       A.[result],
                       A.[timestamp],
                       A.[statusID],
                       A.[approver],
                       A.[approvalDate],
                       B.[FirstName] + ' ' + B.[LastName] AS nomineeName,
                       B.[ntid] AS nomineeNTID,
                       B.[qid] AS nomineeQID,
                       C.[FirstName] + ' ' + C.[LastName] AS submitName,
                       C.[ntid] AS submitNTID,
                       D.[categoryName],
                       (

                       --If exists return true else false
                        select submissionID 
                        from empowermentEntries
                        where sessionID = @session



                       )
                FROM   empowermentSubmissions AS A
                       INNER JOIN
                       empTable AS B
                       ON A.[nomineeEmpID] = B.[empID]
                       INNER JOIN
                       empTable AS C
                       ON A.[subEmpID] = C.[empID]
                       INNER JOIN
                       empowermentCategories AS D
                       ON A.[categoryID] = D.[catID]
                WHERE  (A.[categoryID] = @category or @category = '')
                AND (A.[nomineeDepartment] = @department or @department = '')
                AND A.[submissionDate] >= @startDate and A.[submissionDate] <= @endDate
                FOR    XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');

你会注意到我有一个子选择,我在那里开始这个,然后无法从那里弄清楚。我需要查看提交ID是否存在于另一个表中,如果存在,则返回true,否则返回值为False。

2 个答案:

答案 0 :(得分:0)

SQL Server没有bool类型。你可以用一个案例做到这一点。

CASE WHEN (select count(1) 
from empowermentEntries
where sessionID = @session) > 0 THEN 'true' else 'false' end

如果该行存在,您将得到1或更多的计数,这将在列中放置“true”。如果它不存在,你将返回零,这将返回'false'。

你也可以离开加入empowermentEntries并完全避免使用subselect。

答案 1 :(得分:0)

没有必要使用subselect执行此操作。您正在有效地检查变量@session是否有值。您还可以修改设置该变量的方式更简单。

DECLARE @session INT
SELECT @session = sessionID 
FROM empowermentSessions 
WHERE status = '1'

SELECT A.[submissionID],
       A.[subEmpID],
       A.[nomineeEmpID],
       CONVERT (VARCHAR (10), A.[submissionDate], 101) AS submissionDate,
       A.[situation],
       A.[task],
       A.[action],
       A.[result],
       A.[timestamp],
       A.[statusID],
       A.[approver],
       A.[approvalDate],
       B.[FirstName] + ' ' + B.[LastName] AS nomineeName,
       B.[ntid] AS nomineeNTID,
       B.[qid] AS nomineeQID,
       C.[FirstName] + ' ' + C.[LastName] AS submitName,
       C.[ntid] AS submitNTID,
       D.[categoryName],
       case when @session IS NULL then 'false' else 'true' end
FROM   empowermentSubmissions AS A
       INNER JOIN
       empTable AS B
       ON A.[nomineeEmpID] = B.[empID]
       INNER JOIN
       empTable AS C
       ON A.[subEmpID] = C.[empID]
       INNER JOIN
       empowermentCategories AS D
       ON A.[categoryID] = D.[catID]
WHERE  (A.[categoryID] = @category or @category = '')
AND (A.[nomineeDepartment] = @department or @department = '')
AND A.[submissionDate] >= @startDate and A.[submissionDate] <= @endDate
FOR    XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');