Select all facts even when WHERE clause is present

44 views Asked by At

I know this might sound strange and I can certainly work around my problem. I am using SQL on server side code and a statement is similar to the following:

SELECT myCol FROM mytable WHERE myCol2=url_parameter

url_parameter is a string passed through the URL. Is there any way to set url_parameter to something but still get all facts in the table?

2

There are 2 answers

0
Gordon Linoff On BEST ANSWER

The normal way to handle this is by having a query that looks like:

SELECT myCol
FROM mytable
WHERE myCol2 = url_parameter OR url_parameter IS NULL;  -- or url_parameter = '';

If you were munging the SQL string instead of passing a parameter, you could insert the value myCol2 (without quotes) when you want to get all values. However, you should be using parameters.

1
Mark Adelsberger On

There shouldn't be; if there is, then the server side is written in a way that would allow SQL injection attacks, which is likely a bigger problem than the one you want to solve.

If you are able to modify the server side code, then you could have it respond to some parameter value or combination of parameter values by running a variation of the query that doesn't have the WHERE condition. Or there are other options... but you already said you can work around it, so I assume you have one of those ideas in mind.