I want it to count then if @intcount > 0
it should show data or else no data found, but when I execute it gives me 'no data found' regardless, what am I doing wrong?
@FiscalYear int,
@SchoolID int,
@Status int
AS
BEGIN
SET NOCOUNT ON;
declare @sqlstr varchar(2000)
declare @intCount int
set @intCount = 0
set @sqlstr = 'Select @intCount = Count(*)
From PrivateSchool left outer join Attachment on Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID
inner join FiscalYearPrivateSchool fp ON fp.PrivateSchoolID = PrivateSchool.PrivateSchoolID
Where (FiscalYear = '+convert(varchar, @FiscalYear)+') AND (PrivateSchool.IsActive = 1)'
IF (@SchoolID != -1)
SET @sqlstr = @sqlstr + ' AND SchoolID ='+ convert(varchar, @SchoolID)
IF (@Status = -1)
SET @sqlstr = @sqlstr + ' AND PrivateSchool.PrivateSchoolID = PrivateSchool.PrivateSchoolID'
Else IF (@Status = 1)
SET @sqlstr = @sqlstr + ' AND Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID'
Else
SET @sqlstr = @sqlstr + ' AND Attachment.PrivateSchoolID is Null'
If (@intCount > 0)
BEGIN
set @sqlstr= 'Select SchoolName as School,
(Case when Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID THEN ''Uploaded''
ELSE ''Not Uploaded'' END) AS Status,
COUNT(Attachment.PrivateSchoolID) AS [Count]
From PrivateSchool left outer join Attachment on Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID
inner join FiscalYearPrivateSchool fp ON fp.PrivateSchoolID = PrivateSchool.PrivateSchoolID
Where (FiscalYear = '+convert(varchar, @FiscalYear)+') AND (PrivateSchool.IsActive = 1)'
IF (@SchoolID != -1)
SET @sqlstr = @sqlstr + ' AND SchoolID ='+ convert(varchar, @SchoolID)
IF (@Status = -1)
SET @sqlstr = @sqlstr + ' AND PrivateSchool.PrivateSchoolID = PrivateSchool.PrivateSchoolID'
Else IF (@Status = 1)
SET @sqlstr = @sqlstr + ' AND Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID'
Else
SET @sqlstr = @sqlstr + ' AND Attachment.PrivateSchoolID is Null'
SET @sqlstr = @sqlstr + ' Group by SchoolName, Attachment.PrivateSchoolID, PrivateSchool.PrivateSchoolID'
SET @sqlstr = @sqlstr + ' Order By SchoolName'
EXEC(@sqlstr)
END
ELSE
Select 'No Data Found' as 'FileUpload'
END