I have a select statement. I would like to know if this select statement returns any rows or not. Based on the record count I would like to perform some other action.
I tried using @@RowCount
to get the count but it does not seem to work properly with select statements.
Any suggestions on how this should be used or any other way I could perform my post task after select.
select
isnull(TotalHoursWorkedAmount, 0) / 26
from
EmployeeStatus ESI
where
ESI.EmployeeCode = @employee_id
Adding the other piece
if @@ROWCOUNT=0
SELECT dbo.fxGetAverage(@employee_id)
IF for some reason you cant get
@@rowcount
working you can use this query to check if you are getting any results. using if statement.@@ROWCOUNT
does work with select statements. You did not show your attempt, so I can't really say what you're doing wrong.but anyway, this would provide the rowcount as an example:
or store it in a variable:
And as others stated, if it's just a true/false question - as opposed to do
x
ify
number of rows,z
ifv
number of rows - then you're better off doing an exists, unless you actually need the results of the select.