jeudi 17 janvier 2019

How can I set the WHERE clause to filter on multiple bit columns using only checkboxes that are checked without using dynamic SQL?

I am attempting to filter results of a select statement using checkboxes and parameters with a SqlDataSource control.

The problem is that parameters can't be undefined, and I only want any of the given parameters to equal "1" or "True" if they are defined. Any other time, they shouldn't be used in the WHERE clause at all (or maybe set to something like "IS NOT NULL").

Essentially, when a checkbox is checked, the WHERE clause should include a parameter associated with that checkbox and the value of the parameter should be "1" (True), otherwise, if a checkbox is in any other state, "unchecked" or "indeterminate" the WHERE clause should not include a parameter for that checkbox at all, or it should be set to some value that essentially selects either "1" (True) or "2" (False).

I have tried playing with the WHERE clause by adding parenthesis, adding "AND" and "OR" statements, but I can't seem to figure it out. Dynamic SQL would easily solve this problem, but it would be a nightmare to maintain and it's just bad practice.

As an example, let's say you have the following data set:

| ID  | Col1          | BitField1 | BitField2 | BitField3 |
| 1   | Row1          | 1         | 0         | 1         |
| 2   | Row2          | 1         | 1         | 1         |
| 3   | Row3          | 1         | 0         | 1         |
| 4   | Row4          | 1         | 1         | 0         |
| 5   | Row5          | 0         | 0         | 0         |

On our UX we would have 3 checkboxes, one for each "BitField" in the data.

If the user checks none of the checkboxes, the search result should return:

| ID  | Col1          | BitField1 | BitField2 | BitField3 |
| 1   | Row1          | 1         | 0         | 1         |
| 2   | Row2          | 1         | 1         | 1         |
| 3   | Row3          | 1         | 0         | 1         |
| 4   | Row4          | 1         | 1         | 0         |
| 5   | Row5          | 0         | 0         | 0         |

and the WHERE statement should essentially have no parameters, or if it does, it would be something like WHERE BitField1 IS NOT NULL AND BitField2 IS NOT NULL AND BitField3 IS NOT NULL.

If the user checks the box associated with "BitField2" the result should be:

| ID  | Col1          | BitField1 | BitField2 | BitField3 |
| 2   | Row2          | 1         | 1         | 1         |
| 4   | Row4          | 1         | 1         | 0         |

and the WHERE statement should be WHERE BitField2 = 1.

If the user checks the boxes associated with "BitField1" and "BitField2" the result should be:

| ID  | Col1          | BitField1 | BitField2 | BitField3 |
| 2   | Row2          | 1         | 1         | 1         |
| 4   | Row4          | 1         | 1         | 0         |

and the WHERE statement should be WHERE BitField1 = 1 AND BitField2 = 1.

If the user checks all the boxes, the result should be:

| ID  | Col1          | BitField1 | BitField2 | BitField3 |
| 2   | Row2          | 1         | 1         | 1         |

and the WHERE statement should be WHERE BitField1 = 1 AND BitField2 = 1 AND BitField3 = 1.

Is this something I can do without using dynamic SQL? I'm not using a stored procedure, but a standard TSQL query.

Any help would be GREATLY appreciated.




Aucun commentaire:

Enregistrer un commentaire