(I think) I need to use a CASE statement in a WHERE clause in a Stored Procedure, but I keep getting an error when I try.
I'm writing an SProc to query a users table based on parameters sent to the SP. What I want to be able to do is either select all the variants of a parameter or select a specific one. So, I'm using if statements in my WHERE clause to select whether to use a field or not. This is fine for VARCHAR and INT fields, but I can't seem to come up with a way of doing it for bool fields.
This is what I have so far
What I want to be able to do is use the "active" to pass "y", "n" or "b" to signify active, non active or both user types. Active in the table is a bool field (TINYINT(1)).Code:CREATE PROCEDURE `spGetUser`( id INT, surname VARCHAR (255), forename VARCHAR (255), active CHAR(1), admin CHAR(1), culture VARCHAR(10) ) BEGIN SELECT A.UserName, A.EmailAddress, A.Forename, A.Surname, A.Active, A.Admin, (SELECT CONCAT(B.forename, ' ', B.Surname) FROM tblUsers B WHERE B.ID=A.AddedBy) As AddedbyStr, A.DtAdded, A.Culture FROM tblUsers A WHERE if(id=0, A.ID=A.ID, A.ID=id) AND if(surname='', A.Surname=A.Surname, A.Surname=surname) AND if(forename='',A.forename=A.Forename, A.Forename=forename) ; END $$
How can I use the parameter "active" in my WHERE clause to select whether to show active, non active or both user types?
Hope that makes sense. I'm using MySQL, btw.



LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks