Simple check for SELECT query empty result

SqlSql ServerDatabase

Sql Problem Overview


Can anyone point out how to check if a select query returns non empty result set?

For example I have next query:

SELECT * FROM service s WHERE s.service_id = ?;

Should I do something like next:

ISNULL(SELECT * FROM service s WHERE s.service_id = ?)

to test if result set is not empty?

Sql Solutions


Solution 1 - Sql

IF EXISTS(SELECT * FROM service s WHERE s.service_id = ?)
 BEGIN
   --DO STUFF HERE

 END

Solution 2 - Sql

Use @@ROWCOUNT:

SELECT * FROM service s WHERE s.service_id = ?;

IF @@ROWCOUNT > 0 
   -- do stuff here.....

According to SQL Server Books Online:

> Returns the number of rows affected by > the last statement. If the number of > rows is more than 2 billion, use > ROWCOUNT_BIG.

Solution 3 - Sql

I agree with Ed B. You should use EXISTS method but a more efficient way to do this is:

IF EXISTS(SELECT 1 FROM service s WHERE s.service_id = ?)
BEGIN
   --DO STUFF HERE

END

HTH

Solution 4 - Sql

You can do it in a number of ways.

IF EXISTS(select * from ....)
begin
 -- select * from .... 
end
else
 -- do something 

Or you can use IF NOT EXISTS , @@ROW_COUNT like

select * from ....
IF(@@ROW_COUNT>0)
begin
-- do something
end

Solution 5 - Sql

try:

SELECT * FROM service s WHERE s.service_id = ?;

IF @@ROWCOUNT=0
BEGIN
    PRINT 'no rows!'
END

Solution 6 - Sql

SELECT COUNT(1) FROM service s WHERE s.service_id = ?

Solution 7 - Sql

To summarize the below posts a bit:

If all you care about is if at least one matching row is in the DB then use exists as it is the most efficient way of checking this: it will return true as soon as it finds at least one matching row whereas count, etc will find all matching rows.

If you actually need to use the data for processing or if the query has side effects, or if you need to know the actual total number of rows then checking the ROWCOUNT or count is probably the best way on hand.

Solution 8 - Sql

SELECT * FROM service s WHERE s.service_id = ?;
IF @@rowcount = 0
begin
select 'no data'
end

Solution 9 - Sql

SELECT count(*) as CountThis ....

Then you can compare it as string like so:

IF CHECKROW_RS("CountThis")="0" THEN ...

CHECKROW_RS is an object

Solution 10 - Sql

SELECT count(*) as count FROM service s WHERE s.service_id = ?;

test if count == 0 .

More baroquely:

select case when (SELECT count(*) as count FROM service s WHERE s.service_id = ?) = 0 then 'No rows, bro!' else 'You got data!" end as stupid_message;

Solution 11 - Sql

In my sql use information function

select FOUND_ROWS();

it will return the no. of rows returned by select query.

Solution 12 - Sql

well there is a way to do it a little more code but really effective

$sql = "SELECT * FROM messages";  //your query
$result=$connvar->query($sql);    //$connvar is the connection variable
$flag=0;
     while($rows2=mysqli_fetch_assoc($result2))
    { $flag++;}
    
if($flag==0){no rows selected;}
else{
echo $flag." "."rows are selected"
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDenys S.View Question on Stackoverflow
Solution 1 - SqlEd BView Answer on Stackoverflow
Solution 2 - Sqlmarc_sView Answer on Stackoverflow
Solution 3 - SqlRajaView Answer on Stackoverflow
Solution 4 - SqlSamim HussainView Answer on Stackoverflow
Solution 5 - SqlKM.View Answer on Stackoverflow
Solution 6 - Sqlovais.tariqView Answer on Stackoverflow
Solution 7 - SqlDonnieView Answer on Stackoverflow
Solution 8 - SqlViranja kaushalyaView Answer on Stackoverflow
Solution 9 - Sqlcsandreas1View Answer on Stackoverflow
Solution 10 - SqltpdiView Answer on Stackoverflow
Solution 11 - SqlSunil KumarView Answer on Stackoverflow
Solution 12 - SqlKaran ShrivastavView Answer on Stackoverflow