I have one select query declared in a variable. Now I want to check if the query returns 0 rows then I have to go with other conditions. I am using SQL SERVER 2012. How do I check the variable if it contains 0 rows. Kindly suggest
set @sql = 'select tsf.StaffId,tsf.FullName,tsf.[Address],tsf.PhoneNo,tsf.Email,tsf.Gender,tsf.MobileNo1,tsf.MobileNo2, tsf.Post, tsf.Salary,ts.AdvanceSalary,ts.[Description],ts.[Month] from Tbl_Salary ts
JOIN Tbl_Staff tsf on ts.StaffId = tsf.StaffId'
If @CategoryId is not null
set @sql = @sql + ' where tsf.StaffId='''+cast(@CategoryId as varchar(10))+''''
If @MonthName is not null
set @sql = @sql +' and ts.Month='''+cast(@MonthName as varchar(50))+''''
If @Year is not null
set @sql = @sql +' and ts.Year='''+cast(@Year as varchar(50))+''''
exec(@sql)