存在不正确过滤的SQL

时间:2017-07-26 17:42:18

标签: sql sql-server reporting-services ssrs-2008 ssrs-2012

您好我正在为SSRS报告执行数据驱动订阅,并且只想通过电子邮件向报告实际拥有数据的用户发送电子邮件。我研究了它,发现我需要一个EXISTS条款。但是有些我的声明如何在没有任何数据的情况下撤回数据。

select Companyid,Email 
from [VW_ADMIN] 
where EXISTS(SELECT  
    [RESOURCEID]
    ,[COMPANYNAME]
    ,[COMPANYID]
    ,[QUALIFIED]
    ,[FIRSTNAME]
    ,[MI]
    ,[LASTNAME]
    ,[SUFFIX]
    ,[TYPE]
    ,[LOCATION]
    ,[LICENSENUM]
    ,[QUALIFICATIONID]
    ,[EXPIRATIONDATE]    
    ,[DATE]
    ,[AFBDATE]
    ,[AFADATE]
  FROM [VW_OVERVIEW2]
  where 
    ([COMPANYID]=4)
    and 
    ((EXPIRATIONDATE between (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101)) and (select convert( Varchar(10),(select eomonth(getdate())),101)))
    or ([EXPIRATIONDATE] < (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101)))
    or ([AFCDATE] between (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101)) and (select convert( Varchar(10),(select eomonth(getdate())),101)))
    or ([AFCDATE] < (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101)))
    or ([AFBDATE] between (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101)) and (select convert( Varchar(10),(select eomonth(getdate())),101)))
    or ([AFBDATE] < (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101)))
    or ([AFADATE] between (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101)) and (select convert( Varchar(10),(select eomonth(getdate())),101)))
    or ([AFADATE] < (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101)))
    ))
and active=1 
and CATEGORYID=1 
and roleid=3

当我测试我的报告查询(下面的查询)时,没有记录返回companyid = 4但是对于上面的查询,它返回记录。基本上我将以下报告查询插入上面的where子句

declare @companyid int,@begdate datetime,@enddate datetime;
set @companyid = 4
set @begdate = (Select convert (Varchar(10),(DATEADD(month, DATEDIFF(month, 0, getdate()), 0)),101))
set @enddate = (select convert( Varchar(10),(select eomonth(getdate())),101))
SELECT  
    [RESOURCEID]
    ,[COMPANYNAME]
    ,[COMPANYID]
    ,[QUALIFIED]
    ,[FIRSTNAME]
    ,[MI]
    ,[LASTNAME]
    ,[SUFFIX]
    ,[TYPE]
    ,[LOCATION]
    ,[LICENSENUM]
    ,[QUALIFICATIONID]
    ,[EXPIRATIONDATE]    
    ,[AFCDATE]
    ,[AFBDATE]
    ,[AFADATE]
  FROM [VW_OVERVIEW2]
  where 
    ([COMPANYID]=@COMPANYID)
    and 
    ((EXPIRATIONDATE between @BEGDATE and @ENDDATE)
    or ([EXPIRATIONDATE] < @BEGDATE)
    or ([AFCDATE] between @BEGDATE and @ENDDATE)
    or ([AFCDATE] < @BEGDATE)
    or ([AFBDATE] between @BEGDATE and @ENDDATE)
    or ([AFBDATE] < @BEGDATE)
    or ([AFADATE] between @BEGDATE and @ENDDATE)
    or ([AFADATE] < @BEGDATE)
    )

0 个答案:

没有答案