Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an empty result set?

I'm using a stored procedure in MySQL, with a CASE statement.

In the ELSE clause of the CASE ( equivalent to default: ) I want to select and return an empty result set, thus avoiding to throw an SQL error by not handling the ELSE case, and instead return an empty result set as if a regular query would have returned no rows.

So far I've managed to do so using something like:
Select NULL From users Where False

But I have to name an existing table, like 'users' in this example. It works, but I would prefer a way that doesn't break if eventually the table name used is renamed or dropped.

I've tried Select NULL Where False but it doesn't work.

Using Select NULL does not return an empty set, but one row with a column named NULL and with a NULL value.

like image 831
Petruza Avatar asked Sep 21 '09 18:09

Petruza


People also ask

How do I select Empty data in SQL?

SELECT * FROM yourTableName WHERE yourSpecificColumnName IS NULL OR yourSpecificColumnName = ' '; The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value.

What is SQL empty set?

The empty set is the set containing no elements. This concept is both ubiquitous and extremely important in the relational world, but SQL commits a number of errors in connection with it. Unfortunately there isn't much you can do about most of those errors, but you should at least be aware of them.

Why MySQL returned an empty result set?

An empty result set is returned if there are no rows returned. (An empty result set differs from a null pointer as a return value.) After you have called mysql_store_result() and gotten back a result that is not a null pointer, you can call mysql_num_rows() to find out how many rows are in the result set.

What does empty set mean in MySQL?

If there is 'empty set' in the result set of MySQL query then it means that MySQL is returning no rows and no error also in the query.


2 Answers

There's a dummy-table in MySQL called 'dual', which you should be able to use.

select     1 from     dual where     false 

This will always give you an empty result.

like image 131
Björn Avatar answered Sep 24 '22 17:09

Björn


This should work on most DBs, tested on Postgres and Netezza:

SELECT NULL LIMIT 0; 
like image 37
Aleh Avatar answered Sep 24 '22 17:09

Aleh