Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 572 Vote(s) - 3.63 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple check for SELECT query empty result

#1
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?
Reply

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

END
Reply

#3
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;
Reply

#4
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
Reply

#5
Use @@ROWCOUNT:

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

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

According to [SQL Server Books Online][1]:

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


[1]:

[To see links please register here]

Reply

#6
try:

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

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

#7
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.
Reply

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

#9
In my sql use information function

select FOUND_ROWS();

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

#10
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

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through