对于[Submitted On]
列,当选择@fiscalyear
时,我只希望数据结束该会计年度。
例如,如果@fiscalyear = 2011
我希望[Submitted On]
列的数据仅以2011或null
结尾,同样适用于2010年和2012年的其他年份。目前问题是当我选择2012年0月2010年时,我仍然会得到截止日期为2011年的数据。有什么想法吗?感谢Aaron的暗示 - 我的剧本现在看起来不同了。
@FiscalYear int,
@SchoolID int,
@Status int
AS
BEGIN
SET NOCOUNT ON;
declare @intCount int
declare @sqlstr nvarchar(2000)
set @intCount = 0
Select @intCount = Count(*)
From EnrollmentDateSchool Ed Right Outer Join
(select FP.FiscalYear, PrivateSchool.* from PrivateSchool
INNER JOIN FiscalYearPrivateSchool FP ON PrivateSchool.PrivateSchoolID
= FP.PrivateSchoolID) PS ON Ed.PrivateSchoolID = PS.PrivateSchoolID
Left Outer Join
Finance.dbo.Person P ON Ed.CreatedBy = P.PersonID
WHERE FiscalYear=@FiscalYear AND PS.IsActive=1
AND (@SchoolID = -1 OR SchoolID=@SchoolID)
AND ( (@Status = -1)
OR (@Status=1 AND PS.PrivateSchoolID = Ed.PrivateSchoolID)
OR (Ed.PrivateSchoolID is null) )
IF @intCount > 0
BEGIN
Select
[SchoolName] As [School Name],
Status = CASE WHEN PS.PrivateSchoolID = Ed.PrivateSchoolID
THEN 'Submitted'
ELSE 'Not Submitted'
END,
[Submitted By] = CASE WHEN PS.PrivateSchoolID = Ed.PrivateSchoolID
THEN [FirstName] + ' ' + [LastName]
ELSE NULL
END,
[Submitted On] = CASE WHEN PS.PrivateSchoolID = Ed.PrivateSchoolID
THEN Convert( Varchar(10), Ed.CreatedDate, 101 )
ELSE NULL
END
From EnrollmentDateSchool Ed Right Outer Join
(select FP.FiscalYear, PrivateSchool.*
from PrivateSchool INNER JOIN
FiscalYearPrivateSchool FP ON
PrivateSchool.PrivateSchoolID = FP.PrivateSchoolID) PS ON
Ed.PrivateSchoolID = PS.PrivateSchoolID Left Outer Join
Finance.dbo.Person P ON Ed.CreatedBy = P.PersonID
END
Else Select 'No Data Found' as 'School Roster Certification Report'
END
答案 0 :(得分:0)
之后,您的select语句中的@intCount查询中没有任何where子句。如果您将谓词复制到该查询中(或至少是年份部分),则应该限制您的结果。
答案 1 :(得分:0)
选择COUNT(*)
然后将该结果与0进行比较意味着服务器必须完成所有工作以检索整个结果集,即使它立即找到匹配的行。您可以直接在if语句中使用EXISTS测试替换它,例如:
IF EXISTS (SELECT * FROM /* Current Query */)
BEGIN
END
或者,取决于你如何消耗这个存储过程的结果(我想它已经必须做一些聪明的东西,假设结果可用或不可用的两个可能的结果集具有不同的形状)将是只需要你的内部查询(适当修改),然后在它下面添加:
IF @@ROWCOUNT=0
BEGIN
Select 'No Data Found' as 'School Roster Certification Report'
END
返回受最后一个语句影响的行数。