For the [Submitted On] column, when @fiscalyear is selected, I want only data ending that fiscal year.
For example if @fiscalyear = 2011 I want the data with [Submitted On] column ending with only the 2011 or null and likewise for the other years which is 2010 and 2012. Right now the problem is when i select 2012 0r 2010, I still get data with dates ending in 2011. Any ideas? And thanks to Aaron for the hint – my script looks different now.
@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
Selecting
COUNT(*)and then comparing that result against 0 means that the server has to do all of the work to retrieve the entire result set, even if it immediately found a matching row. You could replace this with an EXISTS test directly in your if statement, e.g.:Alternatively, depending on how your consuming the results from this stored proc (I’d imagine it already has to do some clever stuff, given that the two possible result sets from results available or not available have different shapes) would be to just have your inner query (appropriately modified), and then add this below it:
@@ROWCOUNT: