Is there a variable in SQL that can be used to represent ALL the possible values of a field? Something like this pseudo-code
SELECT name FROM table WHERE id = *ALL_EXISTING_ID-s*
I want to return all rows in this case, but later when I do a search and need only one item I can simply replace that variable with the id I’m looking for, i.e.
SELECT name FROM table WHERE id = 1
The simplest way is to remove the WHERE clause. This will return all rows.
If you want some “magic” value you can use for the ID that you can use in your existing query and it will return all rows, I think you’re out of luck.
Though you could use something like this:
If the value
NULLis provided, all rows will be returned.If you don’t like
NULLthen try the following query, which will return all rows if the value-1is provided:Another approach that achieves the same effect (but requires binding the id twice) is: