开发者

Stored procedure returns inaccurate data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

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.:

IF EXISTS (SELECT * FROM /* Current Query */)
BEGIN

END

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:

IF @@ROWCOUNT=0
BEGIN
    Select 'No Data Found' as 'School Roster Certification Report'
END

@@ROWCOUNT:

Returns the number of rows affected by the last statement.


You don't have any of your where clause from the @intCount query in your select statement after that. If you copy your predicates into that query as well (or at least the Year part), it should limit your results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜