子查询返回的值超过1。我的WHERE子句需要删除SELECT语句

时间:2015-11-18 08:15:52

标签: sql sql-server stored-procedures subquery

我有一个提供问题的SQL存储过程。我知道WHERE子句中的SELECT语句很可能返回多个值。我注释掉了WHERE并返回了结果。

我的问题是如何修改语句,以便我仍然可以过滤这些条件?

存储过程代码:

SELECT
    REPORT_SPOOL.ID, 
    REPORT_SPOOL.REPORT_SPOOL_TYPE_ID, 
    REPORT_SPOOL.FUND_ID, 
    REPORT_SPOOL.PERF_ENTITY_ID, 
    REPORT_SPOOL.REPORT_GUID, 
    REPORT_SPOOL.REPORT_TEMPLATE_GUID, 
    PERF_ENTITY.CODE AS PERF_ENTITY_CODE, 
    PERF_ENTITY.NAME AS PERF_ENTITY_NAME, 
    FUND.CODE AS FUND_CODE, 
    FUND.NAME AS FUND_NAME, 
    REPORT.CODE AS REPORT_CODE, 
    REPORT.NAME AS REPORT_NAME, 
    REPORT_TEMPLATE.CODE AS REPORT_TEMPLATE_CODE, 
    REPORT_TEMPLATE.NAME AS REPORT_TEMPLATE_NAME,
    FUND.ACCOUNTING_START AS START_DATE,
    FUND.ACCOUNTING_END AS END_DATE
FROM
    PERF_ENTITY 
    RIGHT OUTER JOIN
        REPORT 
        INNER JOIN
            REPORT_SPOOL 
            ON REPORT.GUID = REPORT_SPOOL.REPORT_GUID 
        INNER JOIN
            REPORT_TEMPLATE 
            ON REPORT_SPOOL.REPORT_TEMPLATE_GUID = REPORT_TEMPLATE.GUID 
    ON PERF_ENTITY.ID = REPORT_SPOOL.PERF_ENTITY_ID 
    LEFT OUTER JOIN
    FUND 
    ON REPORT_SPOOL.FUND_ID = FUND.ID
    WHERE 
    (END_DATE IS NULL OR END_DATE > @REPORT_DATE)
    AND 
    REPORT_SPOOL.FUND_ID = (SELECT FUND_ID FROM FUND_HLD WHERE [DATE] = @REPORT_DATE)
    AND
    REPORT_SPOOL.FUND_ID = (SELECT FUND_ID FROM FUND_TRD_LINE_VIEW WHERE [DATE] >= (SELECT DATEFROMPARTS(YEAR(@REPORT_DATE),MONTH(@REPORT_DATE),1)) AND [DATE] <= @REPORT_DATE)

最后2个SELECTS是问题

1 个答案:

答案 0 :(得分:2)

在子查询中将=替换为in

REPORT_SPOOL.FUND_ID in (SELECT FUND_ID FROM FUND_HLD WHERE [DATE] = @REPORT_DATE)
    AND
    REPORT_SPOOL.FUND_ID in (SELECT FUND_ID FROM FUND_TRD_LINE_VIEW WHERE [DATE] >= (SELECT DATEFROMPARTS(YEAR(@REPORT_DATE),MONTH(@REPORT_DATE),1)) AND [DATE] <= @REPORT_DATE)